Asp.net mvc HTTP PUT在ASP.NET Web API中不起作用
我有一个简单的Web API控制器,其中Asp.net mvc HTTP PUT在ASP.NET Web API中不起作用,asp.net-mvc,asp.net-mvc-3,asp.net-web-api,asp.net-web-api2,asp.net-web-api-routing,Asp.net Mvc,Asp.net Mvc 3,Asp.net Web Api,Asp.net Web Api2,Asp.net Web Api Routing,我有一个简单的Web API控制器,其中GET和POST工作,但PUT和DELETE不工作。服务器在访问PUT和DELETE时抛出请求的资源不支持http方法“PUT”错误 以下是web.config的外观: <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="entityFramewo
GET
和POST
工作,但PUT
和DELETE
不工作。服务器在访问PUT和DELETE时抛出请求的资源不支持http方法“PUT”错误
以下是web.config的外观:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.5.2">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5.2" />
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
</configuration>
Web API配置代码
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{Id}",
defaults: new { Id = RouteParameter.Optional }
);
}
并且控制器具有…
public class GroupsController : ApiController
{
[System.Web.Http.HttpPut]
public ActionResult Put(int Id, [FromBody]Group NewGroup)
{
//some code here
}
}
我也查看了在线资源,但没有一个能帮上忙。。。
及
我正试图使此API在开发人员机器(VisualStudio附带的IIS express/compact)中工作。似乎您没有将id作为url部分传递。例如,“put/api/Groups”将返回此错误,但“put/api/Groups/1”必须正常工作。您查看过默认路由吗
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routeTemplate:“api/{controller}/{action}/{id}”
配置中缺少{action}
和API,供参考:
[HttpPut]
public IHttpActionResult Hero(int Id)
下面的API调用有效
以下API调用将无法工作,因为API路由不匹配
还要确保从客户端传递的内容类型正确
编辑-2
RESTAPI
属性类
邮递员休息电话
调试器
这样做会引发不同的异常。。。{“Message”:“未找到与请求URI匹配的HTTP资源”。,“MessageDetail”:“在与请求匹配的控制器“组”上未找到任何操作。”}是否确实未重新配置路由或在操作中未更改Id名称?通常,当路由中具有一个名称但控制器名称不同的可选参数时,会发生此错误。例如,路由配置设置了Id参数,但实际上您设置了GroupId。例如,对于您发布的操作public ActionResult Put(int GroupId,[FromBody]Group NewGroup)的路由配置,将抛出此错误。我安装了帮助页面nuget package,这对我很有帮助,此处选择的答案非常接近。{action}缺失,因为REST应该是这样工作的。。。URL和HTTP谓词。操作没有公开。我不接受这个答案,因为在设计REST服务时,{action}不应该是URL的一部分,否则它会破坏约定。
[HttpPut]
public IHttpActionResult Hero(int Id)
[System.Web.Http.HttpPut]
public void Put(int id, Dummy value)
{
}
public class Dummy
{
public string key1 { get; set; }
public string key2 { get; set; }
}