405 方法不允许 Web API 2

Posted

技术标签:

【中文标题】405 方法不允许 Web API 2【英文标题】:405 Method Not Allowed Web API 2 【发布时间】:2016-03-23 05:01:15 【问题描述】:

我几乎浏览了我能找到的所有文章,包括 SO 等等。我对Web.config 进行了更改以满足答案,因为它们似乎都指向删除WebDAV 模块和处理程序。

但是,我仍然收到错误消息:

405 方法不允许

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

注意:这最初只是一个 MVC 4 项目。我添加了工件来支持 Web API。似乎我可能错过了什么。

注意:GET 调用在 Angular 中运行良好。

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

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

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

客户端

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

它有可能落入第二条路线配置吗?如果是这样,我如何验证 .NET 尝试使用的路由配置?

【问题讨论】:

ASP.NET Web API - PUT & DELETE Verbs Not Allowed - IIS 8的可能重复 我会从头开始创建一个 Web.API 应用程序,重新创建问题,看看是否可以重现。看起来您已经完成了正确的 webDAV 配置修改。就像你说的那样,很难判断从 mvc 应用程序转换它是否会导致一些看不见的管道问题。 也许我在这里遗漏了一些东西,但这些路线不会都匹配,这意味着第一条路线会被击中吗?是不是第一个匹配的路由只支持GET? @BrendanGreen 我也想知道同样的事情,但似乎并非如此。我没有收到与多个匹配项相关的错误。我是在添加一个新的之后才知道的,这个错误非常明显。 【参考方案1】:

最后似乎是我试图使路由表复杂化。我最终把它回滚到这个:

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>

【讨论】:

【参考方案2】:

我也为此苦苦挣扎,发现我必须做更多的事情。试试这个:

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

【讨论】:

以上是关于405 方法不允许 Web API 2的主要内容,如果未能解决你的问题,请参考以下文章

Web Api - 不允许使用 Catch 405 方法

405 方法不允许 Web API

在 DELETE 上不允许使用 Owin 自托管 Web API 405 方法

“无法在已发布的 Web API 上发布数据” – 405(不允许的方法)

自定义 Web api 上的 POST 405(不允许的方法)

MVC-Web API:不允许使用 405 方法