zoukankan      html  css  js  c++  java
  • Web API Put Request generates an Http 405 Method Not Allowed error

    Web API Put Request generates an Http 405 Method Not Allowed error

    So, I checked Windows Features to make sure I didn't have this thing called WebDAV installed, and it said I didn't. Anyways, I went ahead and placed the following in my web.config (both front end and WebAPI, just to be sure), and it works now. I placed this inside <system.webServer>.

    <modules runAllManagedModulesForAllRequests="true">
        <remove name="WebDAVModule"/> <!-- add this -->
    </modules>
    

    Additionally, it is often required to add the following to web.config in the handlers. Thanks to Babak

    <handlers>
        <remove name="WebDAV" />
        ...
    </handlers>
    

    405 Method Not Allowed PUT

    Did you decorate your action with HttpPut from the correct namespace? Should look something like this :

    [HttpPut]
    public ActionResult ActionName() 
    {
      //Your code
    }
    

    Edit: Apparently iis express has delete & put disabled by default. If this only happens in iis express you might find this answer usefull. Basicly you have to edit this file :

    %userprofile%documentsiisexpressconfigapplicationhost.config 
    

    On this line:

    <add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    

    And add the verb PUT

    Edit nr 2:
    Following This anwser : open up your iis manager and select you website.
    Click handler mapping link on the right.
    Find the ExtensionlessUrlHandler-Integrated-4.0 entry and double click it.
    In Request Restrictions tab verbs you can then add put to enable put support.

    HttpPut in Asp.net Mvc 5?

    Another possibility is to update the ExtensionlessUrlHandler-Integrated-4.0.

    <system.webServer>
        <handlers>
            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
            <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*"   type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
    </system.webServer>
    

    This solution can be found on: Attribute routing with http PUT method constraint

    上面这个还不起作用的话,可能是因为IIS 7.5导致的问题

    “405 method not allowed” in IIS7.5 for “PUT” method

    回答1

    Often this error is caused by the WebDAV module that try to handle this kind of requests.

    An easy solution is to remove it from modules and from handlers of the system.webServer section just inside your web.config file. Here a configuration example:

    <system.webServer>
        <modules>
            <remove name="WebDAVModule" />
        </modules>
        <handlers>
            <remove name="WebDAV" />
        </handlers>
    </system.webServer>
    

    回答2,如果你确实需要使用WebDAV的话

    I had this problem with WebDAV when hosting a MVC4 WebApi Project. I got around it by adding this line to the web.config:

    <handlers>
      <remove name="WebDAV" />
      <add name="WebDAV" path="*" verb="*" modules="WebDAVModule"
          resourceType="Unspecified" requireAccess="None" />
    </handlers>
    

    As explained here: http://evolutionarydeveloper.blogspot.co.uk/2012/07/method-not-allowed-405-on-iis7-website.html

    回答3

    通过追踪错误提示,得到具体信息

    I enabled the Failed Request Tracing, and got the following info:

     <EventData>
      <Data Name="ContextId">{00000000-0000-0000-0F00-0080000000FA}</Data>
      <Data Name="ModuleName">WebDAVModule</Data>
      <Data Name="Notification">16</Data>
      <Data Name="HttpStatus">405</Data>
      <Data Name="HttpReason">Method Not Allowed</Data>
      <Data Name="HttpSubStatus">0</Data>
      <Data Name="ErrorCode">0</Data>
      <Data Name="ConfigExceptionInfo"></Data>
     </EventData>
    

    So, I uninstalled the WebDAVModule from my IIS, everything is fine now~

    The IIS tracing feature is very helpful.

  • 相关阅读:
    服务器负载均衡的基本功能和实现原理
    几种负载均衡技术的实现
    Postman-CI集成Jenkins
    Postman-进阶
    Postman-简单使用
    chrome安装插件,安装Postman
    关于angularjs dom渲染结束再执行的问题
    Protractor(angular定制的e2e)的简易入门
    关于ng-router嵌套使用和总结
    scss使用后的简单入门总结
  • 原文地址:https://www.cnblogs.com/chucklu/p/14149317.html
Copyright © 2011-2022 走看看