【问题标题】:Asp.Net MVC4 + Web API Controller Delete request >> 404 errorAsp.Net MVC4 + Web API 控制器删除请求 >> 404 错误
【发布时间】:2013-02-28 12:03:57
【问题描述】:

我有一个 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 开发服务器”,但同样的问题。

有什么想法吗?

谢谢。

【问题讨论】:

  • 在您的浏览器中,如果您使用调试工具,您能否捕获 jQuery 实际发出的 URL 并查看它是否正常?

标签: c# asp.net asp.net-mvc asp.net-web-api asp.net-web-api-routing


【解决方案1】:

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

【讨论】:

  • +1 我从一个 MVC 项目开始,但只希望它用于 WebAPI。因此,在删除我认为是 MVC 特定配置后,我的 DELETE 请求是 404ing。谢谢。
【解决方案2】:

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

【讨论】:

【解决方案3】:

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

【讨论】:

    【解决方案4】:

    使用 Dotnet Core 在 Mac 上运行 404。

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

    【讨论】:

    • 很高兴知道。感谢分享。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    相关资源
    最近更新 更多