onsdag 13. juni 2012

Using PUT action with APIController in asp.net mvc 4

I have this very simple PUT Action Method

  1.     public class FeedItemController : ApiController
  2.     {
  3.         [HttpPut()]
  4.         public void Put(int id, string status)
  5.         {
  6.         }
  7.     }

And the corresponding jQuery script:

  1. var url = '@Url.RouteUrl("DefaultApi",new { httproute="", controller="FeedItem", id=2,status="done"})';
  2.    $.ajax({
  3.        url: url,
  4.        type: 'PUT',
  5.        success: function (result) {
  6.            // ...
  7.        }
  8.    });

No matter what I tried I always got a 404 error…
Finally, I gave up changing code and looked at the debugging environment and I found that I was using IIS Express for debugging.

clip_image002

I changed it to “Use Visual Studio Development Server” and voila it worked!

Apparently IIS Express is not configured by default to support the PUT word. To change this open up C:\Users\<YourUserName>\Documents\IISExpress\config\applicationhost.config” and change the line for the ExtensionlessUrl-Integrated-4.0” handler to

 

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

And don’t forget to add the PUT word to your production IIS server ;)