ASP.NET Web API - PUT & DELETE Verbs Not Allowed - IIS 8

前端 未结 19 1897
Happy的楠姐
Happy的楠姐 2020-11-22 05:16

I recently upgraded from Visual Studio 2010 to the Visual Studio 2012 RC. The installer also installs IIS 8 Express which Visual Studio now uses as the default web server.

相关标签:
19条回答
  • 2020-11-22 05:51

    Just a quick update for anyone else who might run into this issue. As of today, changing the %userprofile%\documents\iisexpress\config\applicationhost.config does NOT work any longer (this was working fine until now, not sure if this is due to a Windows update). After hours of frustration, I changed the web.config to add these handlers to system.webserver to get it to work:

    <handlers>
            <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>
    
    0 讨论(0)
  • 2020-11-22 05:51

    Enable CORS (nice and neat)

    1.Add CORS nuget package

    Install-Package microsoft.aspnet.webapi.cors
    

    2.in the WebApiConfig.cs file to Register method add bellow code :

    config.EnableCors();
    

    ex:
    using System.Web.Http;

    namespace test
    {
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
    
    
            config.EnableCors(); //add this**************************
    
    
            // Web API routes
            config.MapHttpAttributeRoutes();
    
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );           
        }
    }
    }
    

    3.Add bellow code into namespace of the controller include get,post,delete,put or any http method

    [EnableCors(origins: "The address from which the request comes", headers: "*", methods: "*")]
    

    ex:

    using System.Web.Http.Cors;//add this******************************
    namespace Test.Controllers
    {
    [EnableCors(origins: "http://localhost:53681/HTML/Restaurant.html", headers: "*", methods: "*")]
    public class RestaurantController : ApiController
    {
        protected TestBusinessLayer DevTestBLL = new TestBusinessLayer();
    
        public List<Restaurant> GET()
        {
            return DevTestBLL.GetRestaurant();
        }
    
        public List<Restaurant> DELETE(int id)
        {
            return DevTestBLL.DeleteRestaurant(id);
        }       
    }
    }
    

    reference :http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

    0 讨论(0)
  • 2020-11-22 05:52

    Here is how you allow extra HTTP Verbs using the IIS Manager GUI.

    1. In IIS Manager, select the site you wish to allow PUT or DELETE for.

    2. Click the "Request Filtering" option. Click the "HTTP Verbs" tab.

    3. Click the "Allow Verb..." link in the sidebar.

    4. In the box that appears type "DELETE", click OK.

    5. Click the "Allow Verb..." link in the sidebar again.

    6. In the box that appears type "PUT", click OK.

    0 讨论(0)
  • 2020-11-22 05:53

    this worked for me on iis8 together with some of the other answers. My error was a 404.6 specifically

    <system.webServer>
      <security>
      <requestFiltering>
        <verbs applyToWebDAV="false">
           <add verb="DELETE" allowed="true" />
        </verbs>
      </requestFiltering>
      </security>
    </system.webServer>
    
    0 讨论(0)
  • 2020-11-22 05:53

    Besides all above solutions, check if you have the "id" or any custom defined parameter in the DELETE method is matching the route config.

    public void Delete(int id)
    {
     //some code here
    }
    

    If you hit with repeated 405 errors better reset the method signature to default as above and try.

    The route config by default will look for id in the URL. So the parameter name id is important here unless you change the route config under App_Start folder.

    You may change the data type of the id though.

    For example the below method should work just fine:

    public void Delete(string id)
    {
     //some code here
    }
    

    Note: Also ensure that you pass the data over the url not the data method that will carry the payload as body content.

    DELETE http://{url}/{action}/{id}
    

    Example:

    DELETE http://localhost/item/1
    

    Hope it helps.

    0 讨论(0)
  • 2020-11-22 05:59

    Okay. I finally got to the bottom of this. You need to jump through some hoops to get the PUT and DELETE verbs working correctly with IIS8. In fact if you install the release candidate of VS 2012 and create a new WEB API project you'll find that the sample PUT and DELETE methods return 404 errors out of the box.

    To use the PUT and DELETE verbs with the Web API you need to edit %userprofile%\documents\iisexpress\config\applicationhost.config and add the verbs to the ExtensionlessUrl handler as follows:

    Change this line:

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

    to:

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

    In addition to the above you should ensure WebDAV is not interfering with your requests. This can be done by commenting out the following lines from applicationhost.config.

    <add name="WebDAVModule" image="%IIS_BIN%\webdav.dll" />
    <add name="WebDAVModule" /> 
    <add name="WebDAV" path="*" verb="PROPFIND,PROPPATCH,MKCOL,PUT,COPY,DELETE,MOVE,LOCK,UNLOCK" modules="WebDAVModule" resourceType="Unspecified" requireAccess="None" />
    

    Also be aware that the default Web API convention is that your method name should be the same as the invoked HTTP verb. For example if you're sending an HTTP delete request your method, by default, should be named Delete.

    0 讨论(0)
提交回复
热议问题