【问题标题】:ASP.NET Web API Custom PUT Method not working (returns 404)ASP.NET Web API 自定义 PUT 方法不起作用(返回 404)
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    相关资源
    最近更新 更多