Asp.Net MVC4 +网页API控制器删除请求>> 404错误 [英] Asp.Net MVC4 + Web API Controller Delete request >> 404 error

查看:150
本文介绍了Asp.Net MVC4 +网页API控制器删除请求>> 404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a VS2012 MVC4 solution where I test Web API Controllers.

我成功地测试了GET,POST,PUT但仍删除了我一个HTTP 404错误。当我在API控制器设置在我的'DeleteMovie行动断点,断点从未达到。

I successfully tested the GET, POST, PUT but the DELETE still got me an http 404 error. When I set a breakpoint in my 'DeleteMovie' action in my api controller, the breakpoint is never reached.

我看了很多帖子关于这个问题,但没有一个人帮我。

I read a lot of posts about this problem but no one helped me.

下面是我对DELETE API控制器:

Here is my API Controller for the DELETE:

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

    }

下面是我的html页面:

Here is my html page:

<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>

我的经典路线如下:

My classic route is as follow:

    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路由如下:

My routing for API is as follow:

    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防爆preSS'检查

In my solution properties, I configure the 'Use local IIS Web server' with 'Use IIS Express' checked

我也试图与使用Visual Studio开发服务器,但是同样的问题。

I also tried with 'Use Visual Studio Development Server' but same problem.

任何想法?

感谢。

推荐答案

HTTP DELETE没有一个机构。你需要通过ID作为查询字符串参数。

HTTP DELETE does not have a body. You need to pass the id as a query string parameter.

这篇关于Asp.Net MVC4 +网页API控制器删除请求&GT;&GT; 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆