【发布时间】:2012-08-20 22:07:19
【问题描述】:
我正在尝试按照http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api 处的说明在我的 API 中创建自定义 PUT 方法。
我的 API:
public class AlarmStatusController : ApiController
{
// Other methods here (removed for brevity)
[HttpPut]
public void ResetAlarmTimeout(long AlarmID)
{
// Do stuff (removed for brevity)
}
}
我对方法的调用:
$.ajax({
type: "PUT",
url: "/api/AlarmStatus/ResetAlarmTimeout",
data: { AlarmID: alarmID },
success: AlarmResetSuccess,
error: AjaxError
});
我在 public static void Register(HttpConfiguration config) 中的 API 路由:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = RouteParameter.Optional, id = RouteParameter.Optional }
);
ajax 调用返回 404。如果我将 API 方法和 ajax 调用更改为 GET,那么它可以工作,但这不是 RESTful,因为我的 GET 方法正在修改对象。
【问题讨论】:
-
您使用什么作为网络服务器 IIS、IIS Express、VS 开发服务器?
-
这可能是因为您的服务器未配置为正确处理 PUT 请求,如果您使用的是 IIS,则需要说明哪个版本以获得有关如何配置的帮助。
-
我正在使用开发服务器(从 Visual Studio 调试)。
-
这不能解决你的问题吗:stackoverflow.com/questions/10099270/…
标签: asp.net-web-api