ASP.NET Web API 中不允许 HTTP PUT [英] HTTP PUT not allowed in ASP.NET Web API

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

问题描述

在我的 Web API 项目中,我无法对我的资源执行 HTTP PUT.我已经阅读了一些类似 问题 这个 问题,我遵循了推荐的建议.

On my Web API project, I cannot perform an HTTP PUT to my resources. I've read through some similar questions on this problem and I've followed the recommended advice.

首先,我在我的机器(Windows 7 64 位)上完全卸载了 WebDAV,然后重新启动了我的机器.

First off, I uninstalled WebDAV completely on my machine (Windows 7 64-bit) and subsequently rebooted my machine.

其次,WebDAV 处理程序在我的 web.config 中被指定为已删除,并且 HTTP PUT 动词被指定为允许用于无扩展 URL 处理程序.

Secondly, the WebDAV handlers were specified as removed in my web.config and the HTTP PUT verb was specified as being allowed for the Extensionless URL Handler.

<modules runAllManagedModulesForAllRequests="false">
  <remove name="WebDAVModule"/>
</modules>

<handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="WebDAV"/>
  <add name="ExtensionlessUrlHandler-Integrated-4.0"
       path="*."
       verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
       type="System.Web.Handlers.TransferRequestHandler"
       resourceType="Unspecified"
       requireAccess="Script"
       preCondition="integratedMode,runtimeVersionv4.0" />
  <add name="AttributeRouting" path="routes.axd" verb="*" type="AttributeRouting.Web.Logging.LogRoutesHandler, AttributeRouting.Web" />
</handlers>

我什至尝试添加 ISAPI 无扩展 URL 处理程序(32 位和 64 位)并将我的应用程序从集成管道应用程序池更改为经典应用程序池.

I even tried adding the ISAPI Extensionless URL Handler (32-bit and 64-bit) and changing my application from the integrated pipeline App Pool to the classic App Pool.

<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit"
      path="*."
      verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
      modules="IsapiModule"
      scriptProcessor="%windir%Microsoft.NETFrameworkv4.0.30319aspnet_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.NETFramework64v4.0.30319aspnet_isapi.dll"
      preCondition="classicMode,runtimeVersionv4.0,bitness64"
      responseBufferLimit="0" />

我目前正在使用 Thinktecture IdentityModel 来启用跨源资源共享 (CORS) 支持.为了我的理智,我选择了启用一切的核心选项,以确保 HTTP PUT 实际上是允许的.

I'm currently using Thinktecture IdentityModel to enable Cross Origin Resource Sharing (CORS) support. For my sanity, I've gone with the nuclear option of enabling everything to make sure the HTTP PUT is actually allowed.

config.RegisterGlobal(httpConfig);

config.ForAllResources()
      .ForAllOrigins()
      .AllowAllMethods()
      .AllowAllRequestHeaders();

属性路由 NuGet 包被配置为从当前程序集中获取所有路由以及 ApiController.

The Attribute Routing NuGet package is configured to pick up all routes from the current assembly and any subtypes of ApiController.

config.AddRoutesFromAssembly(Assembly.GetExecutingAssembly());
config.AddRoutesFromControllersOfType<ApiController>();

我的资源也正确指定了 PUT 属性.

My resource has the PUT attribute properly specified as well.

[PUT("/API/Authenticate/Link/{key}/{identifier}")]
public Boolean LinkUser(Guid key, String identifier) { ... }

我在这件事上查找的每个资源都推荐了完全相同的内容:卸载 WebDAV、禁用 WebDAV 处理程序并确保正确配置无扩展 URL 处理程序.我已经完成了所有这些,但它仍然不起作用.

Every resource I look up on this matter recommends the same exact thing: Uninstalling WebDAV, disabling the WebDAV handlers and making sure that the Extensionless URL handler is properly configured. I've done all that and it still doesn't work.

在 fiddler 中,我得到以下信息:

In fiddler, I'm getting the following:

PUT https://localhost/Test/API/Authenticate/Link/Foo/Bar

{"Message":"The requested resource does not support http method 'PUT'."}

我做错了什么?

推荐答案

显然,AttributeRouting 中存在一个已知问题,其中 HttpPut 方法目前在ASP.NET Web API.

Apparently, there is a known problem within AttributeRouting wherein the HttpPut methods are currently non-functional in ASP.NET Web API.

当前接受的解决方法是在路由上添加适当的动词,直到正确修复出现:

The currently accepted workaround is add the appropriate verb onto the route until a proper fix comes along:

Web API RC 密封了用于路由检测的重要接口底层框架.虽然界面现在是公开的,但变化在 vNext 之前不会发布.所以这里有一些解决方法:

Web API RC sealed a vital interface for route detection by the underlying framework. Though the interface is now public, the change won't be released until vNext. So here are some workarounds:

  • 将 AR 属性与 System.Web.Http 中的 HttpGet、HttpPost、HttpPut 或 HttpDelete 属性结合使用:

[GET("some/url"), HttpGet]
public string Method1() {}

[PUT("some/url"), HttpPut]
public string Method2() {}

[POST("some/url"), HttpPost]
public string Method3() {}

[DELETE("some/url"), HttpDelete]
public string Method4() {}

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

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