405不允许的方法的Web API 2 [英] 405 Method Not Allowed Web API 2

查看:175
本文介绍了405不允许的方法的Web API 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历过几乎每一篇文章我能找到的,对SO等等。我做了更改的Web.config 来满足的答案,因为他们似乎都指向删除的WebDAV 模块和处理程序。

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.

不过,我仍然得到错误:

However, I'm still getting the error:

405不允许的方法

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

The requested resource does not support http method 'PUT'.

注意:这原本只是一个MVC 4项目。我已经添加了文物以支持Web API。好像这是可能的我可​​能错过了一些东西。

NOTE: this was originally just an MVC 4 project. I've added artifacts to support Web API. Seems like it's possible I may have missed something.

注意: GET 呼叫从工作角度就好了

NOTE: the GET calls are working just fine from Angular.

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

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

PUT API Entry

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

客户端

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

是否有可能它落入第二路由配置?如果是这样,我怎么能确认配置.NET正试图用什么路线?

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?

推荐答案

在最后它似乎是我试图路由表复杂的事实。我结束了回滚到这一点:

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

请注意这样的事实,我添加 configuration.MapHttpAttributeRoutes(); 以及

这两个修改后一切工作正常信不信由你。在的Web.config 是这样的:

After those two changes all is working well believe it or not. The Web.config looks like this:

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

这篇关于405不允许的方法的Web API 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆