15

我有一个 VS2012 MVC4 解决方案,我在其中测试 Web API 控制器。

我成功测试了 GET、POST、PUT,但 DELETE 仍然给我一个 http 404 错误。当我在我的 api 控制器中的“删除电影”操作中设置断点时,断点永远不会到达。

我阅读了很多关于这个问题的帖子,但没有人帮助我。

这是我的 DELETE API 控制器:

    [HttpDelete]
    public HttpResponseMessage DeleteMovie(int id)
    {    
        // Delete the movie from the database     
        // Return status code    
        return new HttpResponseMessage(HttpStatusCode.NoContent);

    }

这是我的html页面:

<script type="text/javascript">

    deleteMovie(1, function ()
    {
        alert("Movie deleted!");
    });

    function deleteMovie(id, callback) {
        $.ajax({
            url: "/api/Movie",
            data: JSON.stringify({ id: id }),
            type: "DELETE",
            contentType: "application/json;charset=utf-8",
            statusCode: {
                204: function () {
                    callback();
                }
            }
        });
    }

</script>

我的经典路线如下:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

我的 API 路由如下:

    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "ActionApi", 
            routeTemplate: "api/{controller}/{action}/{id}", 
            defaults: new { id = RouteParameter.Optional }
        );

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

在我的解决方案属性中,我配置了“使用本地 IIS Web 服务器”并选中了“使用 IIS Express”

我也尝试使用“使用 Visual Studio 开发服务器”但同样的问题。

任何的想法?

谢谢。

4

4 回答 4

46

如果您收到的错误是来自 IIS 的 html 内容类型,则错误 404.0

确保您的 web.config 中有由 Web Api 模板添加的部分。默认情况下,IIS 不会提供 DELETE 动词,并且此配置会覆盖该行为。

  <system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
于 2013-04-26T17:01:37.540 回答
18

HTTP DELETE 没有正文。您需要将 id 作为查询字符串参数传递。

于 2013-02-28T12:28:34.823 回答
1

根据 Russell 的回答,我查看了 web config 并在 handlers 方法中找到了两行

....
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />

我已经删除了最后一个并且它有效。

只是客户端上的使用示例(打字稿)

    public deleteComment(commentId: number) {
        var url = 'api/comments/' + commentId;
        return this.$http.delete(url);
    }

和服务器端

    [Route("{id:int}")]
    public async Task<IHttpActionResult> Delete(int id){
        await _snippetService.DeleteComment(id);
        return Ok();
    }
于 2014-11-26T19:34:04.963 回答
1

使用 Dotnet Core 在 Mac 上运行 404。

就我而言,我将属性注释从更改HttpDeleteHttpDelete("{id}")

于 2019-03-30T06:40:12.270 回答