405方法不允许Web API 2

时间:2015-12-16 21:33:09

标签: c# angularjs asp.net-mvc asp.net-web-api2

我已经浏览了几乎所有我能找到的文章,以及更多。我已对 var token = model.Token; var userid = model.UserId; var user = await _userManager.FindByIdAsync(userid); var result = await _userManager.ResetPasswordAsync(user, token, userid); 进行了更改以满足答案,因为它们似乎都指向删除Web.config模块和处理程序。

但是,我仍然收到错误:

  

405方法不允许

     

请求的资源不支持http方法'PUT'。

注意:这最初只是一个MVC 4项目。我添加了工件来支持Web API。好像我可能错过了一些东西。

注意:来自Angular的WebDAV来电正常。

Web API路由配置

GET

的Web.config

configuration.Routes.MapHttpRoute(
    name: "Default API",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { action = "Get", id = RouteParameter.Optional },
    constraints: new { id = @"[\da-z-]{36}" });

configuration.Routes.MapHttpRoute(
    name: "Default API with Action",
    routeTemplate: "api/{controller}/{action}",
    defaults: new { action = "Get" });

var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
formatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();

<modules runAllManagedModulesForAllRequests="true"> <remove name="WebDAVModule" /> </modules> <handlers accessPolicy="Read, Execute, Script"> <remove name="WebDAV" /> <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" /> <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> </handlers> API条目

PUT

客户端

public async Task<IHttpActionResult> Put([FromBody] EditUserViewModel viewModel)

它是否可能属于第二种路线配置?如果是这样,我如何验证.NET尝试使用哪种路由配置?

2 个答案:

答案 0 :(得分:4)

最后,似乎是我试图使路由表复杂化。我最终还是回到了这个:

configuration.MapHttpAttributeRoutes();

configuration.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional });

请注意我添加configuration.MapHttpAttributeRoutes();的事实。

在这两个变化之后,所有人都信不信由你。 Web.config看起来像这样:

<handlers>
  <remove name="ExtensionlessUrl-Integrated-4.0"/>
  <add name="ExtensionlessUrl-Integrated-4.0"
       path="*."
       verb="*"
       type="System.Web.Handlers.TransferRequestHandler"
       preCondition="integratedMode,runtimeVersionv4.0"
       responseBufferLimit="0" />
</handlers>

答案 1 :(得分:0)

我也在努力解决这个问题,并发现我必须多做一点。试一试:

 <handlers>
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <!-- Fix Microsofts propensity to interfere with requests -->
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="SimpleHandlerFactory-ISAPI-2.0-64" />
  <remove name="SimpleHandlerFactory-ISAPI-2.0" />
  <remove name="SimpleHandlerFactory-Integrated" />
  <remove name="SimpleHandlerFactory-Integrated-4.0" />
  <remove name="SimpleHandlerFactory-ISAPI-4.0_64bit" />
  <remove name="SimpleHandlerFactory-ISAPI-4.0_32bit" />
  <!-- IISFIX: Now that we're ripped out everything related to ASP.net, put them back correctly.-->
  <add name="SimpleHandlerFactory-ISAPI-4.0_32bit" path="*.ashx" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="SimpleHandlerFactory-ISAPI-4.0_64bit" path="*.ashx" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="SimpleHandlerFactory-Integrated-4.0" path="*.ashx" verb="*" type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
  <add name="SimpleHandlerFactory-Integrated" path="*.ashx" verb="*" type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
  <add name="SimpleHandlerFactory-ISAPI-2.0" path="*.ashx" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness32" responseBufferLimit="0" />
  <add name="SimpleHandlerFactory-ISAPI-2.0-64" path="*.ashx" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <!--IISFIX: WebDAV is also buggy, and interferes with client requests-->
  <remove name="WebDAV" />
</handlers>