API在一个模型上支持DELETE,但不支持另一个模型

时间:2015-02-11 19:46:21

标签: c# asp.net .net api

我有两个对象的搭建DELETE方法。目前,users对象可以接受DELETE请求并且工作正常,但是requests对象给出了以下消息:

{
"Message": "The requested resource does not support http method 'DELETE'."
}

用户代码和电话是:

// DELETE: api/Users/5
        [Route("{id}"), ResponseType(typeof(User))]
        public IHttpActionResult DeleteUser(string id)
        {
            User user = db.Users.Find(id);
            if (user == null)
            {
                return NotFound();
            }

            db.Users.Remove(user);
            db.SaveChanges();

            return Ok(user);
        }

http://localhost:8080/api/users/2
content-type: application/json
DELETE

请求代码和调用如下:

// DELETE: api/ServiceRequests/5
        [Route("{id}:int"), ResponseType(typeof(ServiceRequest))]
        public IHttpActionResult DeleteServiceRequest(int id)
        {
            ServiceRequest serviceRequest = db.ServiceRequests.Find(id);
            if (serviceRequest == null)
            {
                return NotFound();
            }

            db.ServiceRequests.Remove(serviceRequest);
            db.SaveChanges();

            return Ok(serviceRequest);
        }


http://localhost:8080/api/servicerequests/2
content-type: application/json
DELETE

奇怪的是,如果我将Route("{id}:int")更改为Route("{id}"),它的功能正常,但会消除我的整数约束。

如何允许访问服务请求的DELETE?

1 个答案:

答案 0 :(得分:0)

路由属性语法错误,应该是:

[Route("{id:int}"), ResponseType(typeof(ServiceRequest))]

路线限制应在{}