405 Method Not Allowed using ASP.NET Web API

I have been using the ASP.NET Web API recently on a project and this has been working well together with Backbone.js as the front end framework. I have been using PUT for updating the model and POST for adding to the model.

Everything is going well until I deployed the application on a production server. I got the error when trying to perform a PUT request to the server from the front-end. The error was “405 Method not allowed”.

The reason for this is two parts; one the production server has WebDav installed and this has caused conflicts with ASP.NET Web API and two by default the handler was not allowing the PUT verb.

The solution was to remove the WebDav handler and WebDav module from web.config and change the ExtensionlessUrl handler verb attribute.

1
2
3
4
5
6
7
8
9
10
11
12
<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule" />
    </modules>
    <handlers>
      <remove name="WebDAV" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
   
  </system.webServer>

Passing arrays into a ASP.NET Web Api method as parameters

Here is an easy way to pass in a array of values as parameters in a ASP.NET Web Api method.

1
2
3
4
5
6
        [HttpGet()]
        public HttpResponseMessage FindByMembers([FromUri]Int32[] ids = null)
        {
           //Do stuff
            return Request.CreateResponseMessage(HttpStatusCode.OK);
        }

The Url will be http://mywebsite/api/mycontroller/findbymembers/?ids=1&ids=2&ids=3.

Happy API coding!