Freigeben über


Enabling The PUT Verb with Handlers and IIS 7.0

If you are trying to setup a handler to respond to an HTTP PUT unless you already know how to do it you are likely receiving and HTTP 405 Method Not Allowed error.  This is one of those things that is actually pretty easy once you figure it out.  Basically, you need to make some configuration changes in the web.config.  You can do this through the management tool via the Handler Mappings UI in the IIS Manager:

image

However, for those a little more curious and, in case you don’t have the tool (e.g., running from dev, Azure, Azure dev fabric, etc.)  I’ll talk about the configuration settings for both a generic handler (.ashx) and an IISHandler (usually in a separate dll).

To use a generic handler you’ll need to modify a few places.  First, in the <system.webServer> element you’ll want to add Execute and(or) Write to the accessPolicy attribute of the handlers element depending on what you are doing in your handler:

<handlers accessPolicy=”Read, Execute, Script”>

Within the handlers element you should have an <add /> element whose name is SimpleHandlerFactory-Integrated.  If you are okay with overriding the existing item you and simply add PUT to the verb attribute to get the following:

<add name="SimpleHandlerFactory-Integrated" path="*.ashx" verb="GET,HEAD,POST,DEBUG,PUT" type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />

Notice the PUT verb above.  Now requests for .ashx files should load the .ashx file and execute the ProcessRequest method.  However, take note that the requireAccess attribute is set to “Script”.  If you needed to write via the handler you would need to change that attribute to match the required level of authorization and that authorization would need to be in the accessPolicy as stated above.  In the case that you would like to make use of the SimpleHandlerFactory handler for generic handlers (.ashx), but you wanted to specify the file mask or path or any other specific setting it is probably best to make a copy and rename it thus giving you something like this:

<add name="SimpleHandlerFactory-Integrated-pathed" path="PUTENABLED/*.ashx" verb="GET,HEAD,POST,DEBUG,PUT" type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />

The name attribute must be unique as I did above.  Additionally, you can specify the location to which it responds – take a look at the path attribute.  In this case the handler would be configured only to respond to HTTP PUTs that ask for an .ashx file in the PUTENABLED subfolder of the website.

In the case that you are creating a completely custom handler (IISHandler) you can ignore the above changes and add to the <httpHandlers> element.  It might look something like this:

<httpHandlers>

    <add verb="PUT" path="*" validate="false" type="HandlerLibrary.IISHandler1, HandlerLibrary, PublicKeyToken=04b425125eee45fb" />
</httpHandlers>

Just for ease of reading I left out the other config info, but you’ll notice this is similar to the afore mentioned configuration except that you have to use the above syntax to tell IIS to what type to load, the verb(s) to which the handler responds, and the path (this can include path and file mask).

You may have to make some other user or security changes depending on what you need to do within the handler, but the above elements should help move you a long.

Comments

  • Anonymous
    July 06, 2010
    Whenever I add <add verb="PUT" path="*" validate="false" type="HandlerLibrary.IISHandler1, HandlerLibrary, PublicKeyToken=04b425125eee45fb" />  in the httphandlers section, azure development fabric fails to start. It keeps repeating the following sequence "starting, busy, stopping. Any clue how to solve this? Thanks in advance

  • Anonymous
    October 10, 2010
    This post was for IIS 7 not for Azure.  You can add a module, but I'm not sure about a handler, because currently it isn't IIS 7.  If you did get it working, post it up.  If not, I suggest changing to an HttpModule for the time being.

  • Anonymous
    June 11, 2012
    Can you please suggest solution for IIS 7.5, I m generating a HTTPwebrequest (with delete method) for deleting an xml file after the download.

  • Anonymous
    February 15, 2015
    Looks like you need to remove WebDAV from the modules and handlers... stackoverflow.com/.../405-method-not-allowed-in-iis7-5-for-put-method