未在DELETE命令到达的WebAPI控制器 [英] WebAPI Controller is not being reached on DELETE command

查看:229
本文介绍了未在DELETE命令到达的WebAPI控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有困难让我的控制器DELETE方法提交过的ASP.NET Web API请求时开火。它返回一个404,但我想不出为什么。获取和放大器; POST请求按预期方式工作,当提供一个id返回既有的产品清单,以及单个项目的,但是当我打电话使用DELETE请求的API,我得到一个404错误。

I am having difficulty getting the DELETE Method on my Controller to fire when submitting the request over ASP.NET Web API. It returns a 404 but I cannot figure out why. The GET & POST requests work as expected, returning both a list of items as well as a single item when provided an id, but when I call the API using a DELETE request I get a 404 ERROR.

情景:

不是一个MVC应用程序,虽然我为了利用Web API功能已经安装了MVC4包。

2。在Global.asax中

路由表定义

Not an MVC application although I have installed the MVC4 package in order to leverage the Web API features.

            RouteTable.Routes.MapHttpRoute(

                    "Default", 
                    "api/{controller}/{id}", 
                    new { id = RouteParameter.Optional } 
            );

3。控制器定义

    public HttpResponseMessage<Customer> Post(Customer customer)
    {
        CustomerDb.Customers.AddObject(customer);
        CustomerDb.SaveChanges();
        var response = new HttpResponseMessage<Customer>(customer, HttpStatusCode.Created);
        response.Headers.Location = new Uri(Request.RequestUri, "/api/Customer/"+customer.id.ToString());
        return response;
    }

    public CustomerDTO Get(int id)
    {
        CustomerDTO custDTO = null;
        Customer cust = CustomerDb.Customers.Where(c => c.id == id).SingleOrDefault();
        if (cust == null)
            throw new HttpResponseException(HttpStatusCode.BadRequest);
        else
            custDTO = new CustomerDTO(cust);
        return custDTO;
    }

    public IEnumerable<CustomerDTO> Get()
    {
        IQueryable<Customer> custs = CustomerDb.Customers.AsQueryable();

        List<CustomerDTO> dto = new List<CustomerDTO>();
        foreach (Customer cust in custs)
        {
            dto.Add(new CustomerDTO(cust));
        }

        return dto;
    }

    public Customer Delete(int id)
    {
        Customer cust = CustomerDb.Customers.Where(c => c.id == id).SingleOrDefault();
        if (cust == null)
            throw new HttpResponseException(HttpStatusCode.BadRequest);

        CustomerDb.Customers.DeleteObject(cust);
        CustomerDb.SaveChanges();
        return (cust);
    }

我有一些抛出错误请求错误,而不是404的方法,当客户无法找到,所以我不明白这些反应与实际问题相混淆。显然,在实际的实施没有客户会返回一个404错误。

I have some of the methods throwing a BadRequest error instead of a 404 when a customer cannot be found so I don't get these responses confused with the REAL problem. Obviously in a real implementation a no customer would return a 404 error.

function deleteCustomer(id) {

        var apiUrl = "/api/customer/{0}";
        apiUrl = apiUrl.replace("{0}", id);

        $.ajax({
            url: apiUrl,
            type: 'DELETE',
            cache: false,
            statusCode: {
                200: function (data) {
                }, // Successful DELETE
                404: function (data) {
                    alert(apiUrl + " ... Not Found");
                }, // 404 Not Found
                400: function (data) {
                    alert("Bad Request O");
                } // 400 Bad Request
            } // statusCode
        }); // ajax call
    };

所以,我希望是,辛格运河路线图应该容纳所有的方案...

SO I am expecting that the singel route map should accomodate ALL the scenarios ...


  1. 获取API /客户 - 返回所有客户

  2. 获取API /客户/ 5 - 返回其ID = 5
  3. 客户
  4. POST API /客户 - 创建一个新的客户记录

  5. 删除API /客户/ 5 - 删除其ID的客户= 5

1,2&放大器;没有问题3个工作,只是DELET不起作用。我已经尝试了许多次迭代和不同的调整,都无济于事。我还是觉得不过,我可以俯瞰东西不多。我觉得这个问题必须围绕theRoute映射,但我不明白为什么这条路就不会成功地处理删除请求。

1,2 & 3 work without a problem, just the DELET does not work. I have tried MANY iterations and different tweaks, to no avail. I still feel however that I am overlooking something small. I feel like the problem must be around theRoute mapping but I don't see why this route would not succesfully handle the DELETE request.

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

感谢您!

加里

推荐答案

您已经在这你的web.config中定义?

Do you have this defined in your web.config?

   <system.webServer>
          <modules runAllManagedModulesForAllRequests="true">
          </modules>
    </system.webServer>

这篇关于未在DELETE命令到达的WebAPI控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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