简体   繁体   English

405方法不允许Web API 2

[英]405 Method Not Allowed Web API 2

I've been through just about every article I can find, on SO and more. 我已经浏览了几乎所有我能找到的文章,以及更多。 I've made changes to the Web.config to meet the answers as they all seem to point to removing the WebDAV module and handler. 我已经对Web.config进行了更改以满足答案,因为它们似乎都指向删除WebDAV模块和处理程序。

However, I'm still getting the error: 但是,我仍然收到错误:

405 Method Not Allowed 405方法不允许

The requested resource does not support http method 'PUT'. 请求的资源不支持http方法'PUT'。

NOTE: this was originally just an MVC 4 project. 注意:这最初只是一个MVC 4项目。 I've added artifacts to support Web API. 我添加了工件来支持Web API。 Seems like it's possible I may have missed something. 好像我可能错过了一些东西。

NOTE: the GET calls are working just fine from Angular. 注意: GET调用在Angular中工作得很好。

Web API Route Config Web API路由配置

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();

Web.config Web.config文件

<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>

PUT API Entry PUT API条目

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

Client Side 客户端

vm.model.$update(function () {
    $state.go(userStates.app);
});

Is it possible it's falling into the second route configuration? 是否有可能陷入第二种路线配置? If so, how can I verify what route configuration .NET is trying to use? 如果是这样,我如何验证.NET尝试使用的路由配置?

In the end it appears to be the fact that I was trying to complicate the routing table. 最后,似乎是我试图使路由表复杂化。 I ended up rolling it back to this: 我最后回滚到这个:

configuration.MapHttpAttributeRoutes();

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

Do take note to the fact that I added configuration.MapHttpAttributeRoutes(); 请注意我添加了configuration.MapHttpAttributeRoutes(); as well. 同样。

After those two changes all is working well believe it or not. 在这两个变化之后,一切都运转良好,信不信由你。 The Web.config looks like this: 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>

I struggled with this as well and found that I had to do a little bit more. 我也在努力解决这个问题,并发现我必须多做一点。 Give this a try: 尝试一下:

 <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM