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.

4

1 回答 1

2

你用的是VS2010吗?如果是这样,您可能正在使用 AFAIK 不支持的Cassini PUT(请参阅相关的 Cassini 和 IISExpress PUT/DELETE Verbs cause 405 Http Code)。IISExpress 与 VS2010 配合得很好,支持PUTDELETE动词等,还有其他优点,所以我会考虑安装它并使用它。

于 2012-08-25T19:30:32.997 回答