15

I'm working on an MVC application. On my original draft of the service I had this method in one of my controllers:

    [AcceptVerbs(HttpVerbs.Post)]
    [ActionName("UpdateRelationship")]
    public ActionResult UpdateRelationship(string aParameter)

And that worked fine. In the latest revision I've been asked to change this into a PUT request to differentiate it from a similar add mechanism which uses post. So I changed it to this:

    [AcceptVerbs(HttpVerbs.Put)]
    [ActionName("UpdateRelationship")]
    public ActionResult UpdateRelationship(string aParameter)

And suddenly I'm getting a 404 on my requests, all from just changing the AcceptVerbs. From the look of the error, it appears as though IIS is trying to route the request as a standard webforms page rather than using MVC extensionless url re-writing.

Googling this it seems that a common cause is browsers not allowing PUT requests but I'm not using a browser to test this - I'm using Fiddler. So there should be no problem there. I also think the correct settings are already in the web.config:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
  <remove name="WebDAVModule" />
    </modules>
    <handlers>
        <remove name="UrlRoutingHandler" />
        <remove name="MvcHttpHandler" />
  <remove name="WebDAV" />
        <add name="MvcHttpHandler" preCondition="integratedMode" verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </handlers>
    <security>
        <requestFiltering>
            <verbs>
                <add verb="PUT" allowed="true" />
            </verbs>
        </requestFiltering>
    </security>
</system.webServer>

So what have I missed?

EDIT: This code works on a colleagues' machine. So it looks like my local setup of IIS as at fault. Still at a loss to explain what I need to change though - any ideas?

Cheers, Matt

tereško
  • 58,060
  • 25
  • 98
  • 150
Bob Tway
  • 9,301
  • 17
  • 80
  • 162

3 Answers3

10

I had to remove the WebDav Module completely a stated in this blog post

<configuration>
  <system.webServer>
    <handlers>
      <remove name="WebDAV" />
      <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>
    <modules>
      <remove name="WebDAVModule" />
    </modules>
  </system.webServer>
</configuration>
David
  • 581
  • 6
  • 15
  • This solved my issue on GoDaddy shared hosting. By default only GET and POST are enabled but this allowed PUT and DELETE (and HEAD) for my REST api. – Scott Rickman Sep 27 '15 at 10:42
  • Don´t forget to remove the module WebDAVModule like this post says, that is the reason it was not working in GoDaddy host. – Lelis718 Jul 12 '16 at 18:10
  • 1
    After almost two days fighting this problem, you saved my life. Thanks man. This really solved my problem with GO_TRASH_DADDY. – Gerson C Filho Oct 17 '16 at 01:35
5

After much fruitless searching and blind alleys involving WebDAV I found the answer on another SO family site :)

https://serverfault.com/questions/93424/how-to-enable-put-and-delete-in-iis7

Community
  • 1
  • 1
Bob Tway
  • 9,301
  • 17
  • 80
  • 162
  • 1
    What extension you updated? The answer says to udpdae any extension (.aspx/.ascx), for MVC, there is no such extension. Can you elaborate your answer ? – Shyju Sep 07 '12 at 18:00
3

The configuration that worked for us was the following.

    <system.webServer>
    <modules runAllManagedModulesForAllRequests="false">
        <remove name="UrlRoutingModule" />
        <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" preCondition="" />
    </modules>
    <handlers>
        <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
        <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,POST,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.webServer>

This is specific to extension-less URLs.

BTW the general recommendation is to set runAllManagedModulesForAllRequests = false.