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

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

问题描述

在我的Web API项目,我不能执行 HTTP PUT 我的资源。我已经通过<读href=\"http://stackoverflow.com/questions/10906411/asp-net-web-api-put-delete-verbs-not-allowed-iis-8\">some <一href=\"http://stackoverflow.com/questions/11727525/asp-net-mvc-web-api-http-put-and-delete-requests-failing\">similar <一href=\"http://stackoverflow.com/questions/9854602/asp-net-web-api-405-http-verb-used-to-access-this-page-is-not-allowed-how\">questions <一href=\"http://stackoverflow.com/questions/10099270/asp-net-web-api-returns-404-for-put-only-on-some-servers?rq=1\">on <一href=\"http://stackoverflow.com/questions/12136143/put-and-delete-returns-404-for-asp-net-webapi-in-windows-2008-server-iis-7-and-7?rq=1\">this <一href=\"http://geekswithblogs.net/michelotti/archive/2011/05/28/resolve-404-in-iis-ex$p$pss-for-put-and-delete-verbs.aspx\">problem我已经遵循了推荐意见。

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

其次,是在我的的web.config中删除并指定了 HTTP PUT 动词指定的WebDAV的处理程序作为被允许的扩展名的URL处理程序。

 &LT;模块runAllManagedModulesForAllRequests =false的&GT;
  &LT;清除NAME =WebDAVModule/&GT;
&LT; /模块&gt;&LT;&处理GT;
  &LT;清除NAME =ExtensionlessUrlHandler-ISAPI-4.0_32bit/&GT;
  &LT;清除NAME =ExtensionlessUrlHandler-ISAPI-4.0_64bit/&GT;
  &LT;清除NAME =ExtensionlessUrlHandler - 集成 - 4.0/&GT;
  &LT;清除NAME =WebDAV的/&GT;
  &LT;添加名称=ExtensionlessUrlHandler - 集成 - 4.0
       路径=*。
       动词=GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS
       类型=System.Web.Handlers.TransferRequestHandler
       = resourceType为未指定
       requireAccess =脚本
       preCondition =integratedMode,runtimeVersionv4.0/&GT;
  &LT;添加名称=AttributeRouting路径=routes.axd动词=*TYPE =AttributeRouting.Web.Logging.LogRoutesHandler,AttributeRouting.Web/&GT;
&LT; /处理器&GT;

我甚至尝试添加的ISAPI扩展名的URL处理程序(32位和64位)和改变从综合管线应用程序池经典的应用程序池我的申请。

 &LT;添加名称=ExtensionlessUrlHandler-ISAPI-4.0_32bit
      路径=*。
      动词=GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS
      模块=IsapiModule
      scriptProcessor ​​=%WINDIR%\\ Microsoft.NET \\框架\\ v4.0.30319 \\ ASPNET_ISAPI.DLL
      preCondition =classicMode,runtimeVersionv4.0,bitness32
      responseBufferLimit =0/&GT;
&LT;添加名称=ExtensionlessUrlHandler-ISAPI-4.0_64bit
      路径=*。
      动词=GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS
      模块=IsapiModule
      scriptProcessor ​​=%WINDIR%\\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ ASPNET_ISAPI.DLL
      preCondition =classicMode,runtimeVersionv4.0,bitness64
      responseBufferLimit =0/&GT;

我目前使用 Thinktecture IdentityModel 启用跨源资源共享(CORS)的支持。对于我的理智,我已经用使一切以确保 HTTP PUT 实际上是允许的核选择。

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

借助属性路由的NuGet程序包配置拿起从目前装配的所有路由和 ApiController <任何亚型/ code>。

  config.AddRoutesFromAssembly(Assembly.GetExecutingAssembly());
config.AddRoutesFromControllersOfType&LT; ApiController&GT;();

我的资源有 PUT 属性正确指定为好。

  [PUT(/ API /身份验证/连接/ {键} / {}标识符)]
公共布尔LinkUser(GUID键,字符串标识符){...}

每个资源我期待在这个问题上建议同样的事情:卸载的WebDAV,禁用​​WebDAV的处理程序,并确保该扩展名的URL处理程序的配置是否正确。我所做的一切,它的还是不起作用。

在小提琴手,我得到如下:

  PUT的https://本地主机/测试/ API /身份验证/连接/美孚/酒吧{消息:所请求的资源不支持HTTP方法把'。}

我在做什么错了?


解决方案

显然,可能是在一个已知的问题 AttributeRouting ,其中 HttpPut 方法是目前的ASP.NET Web API不起作用。

借助目前公认的解决办法是添加相应的动词上的路线,直至适当的修正到来:


  

网页API RC密封路由检测起着重要的桥梁由
  基本框架。虽然接口现在是公开的,变化
  将不会被释放,直到vNext。因此,这里有一些解决方法:


  
  

      
  • 使用AR结合属性与HTTPGET,HttpPost,HttpPut或HttpDelete从System.Web.Http属性:

  •   

  [GET(有些/ URL),HTTPGET]
公共字符串方法1(){}[PUT(有些/ URL),HttpPut]
公共字符串方法2(){}[POST(有些/ URL),HttpPost]
公共字符串Method3(){}[删除(有些/ URL),HttpDelete]
公共字符串Method4(){}

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.

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

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>

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.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" />

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

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

My resource has the PUT attribute properly specified as well.

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

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.

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'."}

What am I doing wrong?

解决方案

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

  • Use AR attributes in combination with HttpGet, HttpPost, HttpPut, or HttpDelete attributes from System.Web.Http:

[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() {}

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

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