Asp.Net MVC4 + Web API控制器删除请求>> 404错误

问题描述 投票:11回答:4

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

我成功测试了GET,POST,PUT但是DELETE仍然给我一个http 404错误。当我在api控制器中的'DeleteMovie'动作中设置断点时,永远不会到达断点。

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

这是我的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开发服务器'但同样的问题。

任何的想法?

谢谢。

c# asp.net asp.net-mvc asp.net-web-api asp.net-web-api-routing
4个回答
18
投票

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


39
投票

如果您收到的错误是来自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>

0
投票

根据Russell的回答,我查看了web配置,并在处理程序方法中找到了两行

....
<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();
    }

0
投票

使用Dotnet Core在Mac上运行404。

在我的例子中,我将属性注释从HttpDelete更改为HttpDelete("{id}")

© www.soinside.com 2019 - 2024. All rights reserved.