How do I enable HTTP PUT and DELETE for ASP.NET MVC in IIS?


Go to Handler Mappings in your IIS Manager. Find ExtensionlessUrlHandler-Integrated-4.0, double click it. Click Request Restrictions… button and on Verbs tab, add both DELETE and PUT. enter image description here

EDIT: Possible WebDav Publisher issue

You’ve mention on a deleted post you were running on a 2008 server right? Try removing webDav role, or disable it from your site config: on system.webServer -> modules section, remove WebDAVModule module:

    <remove name="WebDAVModule" />
    <remove name="WebDAV" />

Asp.NET Web API – 405 – HTTP verb used to access this page is not allowed – how to set handler mappings


I wrote REST service using ASP.NET Web API. I’m trying to send HttpDelete request, however I get the following error:

405 - HTTP verb used to access this page is not allowed

I think I’m close to the solution, I found out that I should enable IIS remote management , go to Handler Mappings section and add DELETE verb to the appropriate position… but the problem is that there is a lots of different positions on the list… (sth like here:

Which one should I edit? Few of them don’t have extension, e.g. “ExtensionUrlHandler-Integrated-4.0” and I added DELETE verb to it, but it still doesn’t work…

It was just a shot in the dark to modify that one, so should I modify different position? If so, which one? Or maybe is there anything more what I should do?

The same web service work perfectly fine on my local service, so I guess the problem is with the remote IIS…



Common cause for this error is WebDAV. Make sure you uninstall it.


You don’t need to uninstall WebDAV, just add these lines to the web.config:

    <remove name="WebDAVModule" />
    <remove name="WebDAV" />

Percentage Encoding of special characters before sending it in the URL


I need to pass special characters like #,! etc in URL to Facebook,Twitter and such social sites. For that I am replacing such characters with URL Escape Codes.

return valToEncode.Replace("!", "%21").Replace("#", "%23") .Replace("$", "%24").Replace("&amp;", "%26") .Replace("'", "%27").Replace("(", "%28") .Replace(")", "%29").Replace("*", "%2A");

It works for me, but I want to do it more efficiently.Is there any other way to escape such characters? I tried with Server.URLEncode() but Facebook doesn’t render it.

Thanks in advance,


You should use the Uri.EscapeDataString method if you want to have compatibility with RFC3986 standard, where percent-encoding is defined.

For example spaces always will be encoded as %20 character:

var result = Uri.EscapeDataString("a q");
// result == "a%20q"

while for example usage of HttpUtility.UrlEncode (which is by the way internally used by HttpServerUtility.UrlEncode) returns + character:

var result = HttpUtility.UrlEncode("a q") 
// result == "a+q"

What’s more, the behavior of Uri.EscapeDataString is compatible with client side encodeURIComponent javascript method (except the case sensitivity, but RFC3986 says it is irrelevant).


Return either xml or json from MVC web api based on request

Given the following webapiconfig;

            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }

and this controller;

public class ProductsController : ApiController
    Product[] _products = new Product[] 
        new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
        new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
        new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 

    public IEnumerable<Product> GetAllProducts()
        return _products;

Using the URL http://localhost/api/Products I get a list of products in XML format.

What I would like to do is have the option to return either json or xml based on the request. So for json, it would be;


and for XML, it would be;




Is this possible and how would I achieve this functionality?

An alternative would be something like;


Yes you can achieve that with AddUriPathExtensionMapping

You can create routes like this:

  name: "Api UriPathExtension",
  routeTemplate: "api/{controller}.{extension}/{id}",
  defaults: new { id = RouteParameter.Optional, extension = RouteParameter.Optional }

  name: "Api UriPathExtension ID",
  routeTemplate: "api/{controller}/{id}.{extension}",
  defaults: new { id = RouteParameter.Optional, extension = RouteParameter.Optional }

Then you need to extend the formatters:

  config.Formatters.JsonFormatter.AddUriPathExtensionMapping("json", "application/json");
  config.Formatters.XmlFormatter.AddUriPathExtensionMapping("xml", "text/xml");

Make sure to add reference to System.Net.Http.Formatting, as these methods are extension methods and intellisense won’t see them by default.

Remember in this example, you still have to issue the request with the appropriate content-type. However, if you want to have these directly available via the browser address bar you can map to “text/html”.

I wrote a blog post about all that a while ago – which should be helpful and take you into more details


WebAPI cannot parse multipart/form-data post


I’m trying to accept a post from a client (iOS app) and my code keeps failing on reading the stream. Says the message is not complete. I’ve been trying to get this working for hours it seems like something is wrong in my message format or something. All I’m trying to do is read a string but the developer I’m working with who is doing the iOS part only knows how to send multipart/form-data not content-type json.

Here is exact error: Unexpected end of MIME multipart stream. MIME multipart message is not complete.”

It fails here: await Request.Content.ReadAsMultipartAsync(provider);


POST http://localhost:8603/api/login HTTP/1.1
Host: localhost:8603
Accept-Encoding: gzip,deflate
Content-Type: multipart/form-data; boundary=------------nx-oauth216807
Content-Length: 364
Accept-Language: en-us
Accept: */*
Connection: keep-alive


Content-Disposition: form-data; name="token"


Here is the WebAPI code:

    public async Task&lt;HttpResponseMessage&gt; PostFormData()
        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        // Read the form data and return an async task.
        await Request.Content.ReadAsMultipartAsync(provider);

        // This illustrates how to get the file names.
        foreach (MultipartFileData file in provider.FileData)
            Trace.WriteLine("Server file path: " + file.LocalFileName);
        return Request.CreateResponse(HttpStatusCode.OK);

        catch (System.Exception e)
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);

A \r\n needs to be appended at the end of request content stream.

Instead of using this line to read the data: await Request.Content.ReadAsMultipartAsync(provider);

You will need to:
1. load the request stream to memory
2. append the \r\n string that is required
3. create a stream content from the memory content
4. manually add the request headers to the stream content
5. Finally use this instead: streamContent.ReadAsMultipartAsync(provider);

Check the answer of Landuber Kassa here for the complete code:

Modify the Request Queue Limit, requestQueueLimit or Queue Length in IIS 7


November 14, 2011

When optimizing and tuning an ASP.NET application you may want to increase the value of the requestQueueLimit.  The requestQueueLimit is the maximum number of requests that can be queued by an ASP.NET process before errors get returned to the client.

Modify Request Queue Limit within the Aspnet.config

Below, the requestQueueLimit is configured in the Aspnet.config file for a 64 bit version of the .NET 4 Framework.  The Aspnet.config exists by default, for 64bit .Net 4, in the following directory:


Adding the applicationPool element to the Aspnet.config, as shown in Figure 1, for the given .NET version means that all application pools running on IIS 7 in Integrated mode using this version of the .NET framework will be impacted by the modification.

Figure 1, settings for the applicationPool element.

Modify Queue Length for a given Application Pool

Below, shown in Figure 2, the Queue Length is configured from the Advanced Settings of a specific Application Pool.

Figure 2, settings for the application pool

The Queue Length is the maximum number of requests that will be queued, when exceeded new requests will receive a 503 “Service Unavailable” response.


In most cases the default configuration will work best, therefore only make modifications when you are certain of the need and impact.

“The type or namespace name ‘Route’ could not be found” using “attribute routing”


Just trying to splice some code from one working project to another. The “from” project uses “attribute routing” where you embed [Route(…)] directives in the Web API controller modules to indicate what HTTP message should route to what service routine.

Works fine in the “from” project, but in the “to” project I get the build error “The type or namespace name ‘Route’ could not be found (are you missing a using directive or an assembly reference?)”

I’ve tried copying essentially all of the using statements from the “from” project to the “to” project, but that has no apparent effect. None of the MS documentation suggests that a NuGet package is required (or even a using statement). Both projects are supposedly ASP.NET MVC 4.

(And, yes, I updated WebApiConfig.cs with the config.MapHttpAttributeRoutes(); statement.)

Any ideas??


This comment from Vedran Mandić solved the problem for me. I’m re-posting it here because I think it should be the answer (or at least a answer).

I did an ‘Update-Package Microsoft.AspNet.WebApi.WebHost -reinstall’ and it worked. Funny this happens after getting the latest version on different PCs from TFS. I guess this happens because of nuget packages not working properly with the versioning system