3

I am trying to create a custom PUT method in my API, following the instructions at http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api.

My API:

public class AlarmStatusController : ApiController
{
    // Other methods here (removed for brevity)

    [HttpPut]
    public void ResetAlarmTimeout(long AlarmID)
    {
        // Do stuff (removed for brevity)
    }
}

My call to the method:

$.ajax({
    type: "PUT",
    url: "/api/AlarmStatus/ResetAlarmTimeout",
    data: { AlarmID: alarmID },
    success: AlarmResetSuccess,
    error: AjaxError
});

My API route in public static void Register(HttpConfiguration config):

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { action = RouteParameter.Optional, id = RouteParameter.Optional }
);

The ajax call returns 404. If I change the API method and ajax call to a GET, then it works, but this isn't RESTful since my GET method is modifying the object.

Darrel Miller
  • 129,370
  • 30
  • 183
  • 235
datadamnation
  • 1,552
  • 2
  • 13
  • 17
  • What are you using as a webserver IIS, IIS Express, VS Development server? – nemesv Aug 25 '12 at 18:48
  • It's probably because your server isn't configured to correctly handle PUT requests, if you're using IIS, you'll need to say which version for help on how to configure. – joocer Aug 25 '12 at 19:03
  • I am using the dev server (debug from Visual Studio). – datadamnation Aug 25 '12 at 19:12
  • Doesnt this solve your issue: http://stackoverflow.com/questions/10099270/asp-net-web-api-returns-404-for-put-only-on-some-servers – Sando Aug 27 '12 at 11:52

1 Answers1

2

Are you using VS2010? If so you're probably using Cassini, which AFAIK doesn't support PUT (see related Cassini and IISExpress PUT/DELETE Verbs cause 405 Http Code). IISExpress works well with VS2010, supports the PUT and DELETE verbs, etc. and has other advantages, so I'd consider installing that and using it.

Community
  • 1
  • 1
JayC
  • 6,903
  • 2
  • 23
  • 40