不支持asp.net web api PUT方法

时间:2014-08-16 09:36:29

标签: asp.net asp.net-web-api iis-express

我正在为PersonController发出PUT请求,但它甚至没有到达控制器。相反,我得到了服务器

的响应
{"message":"The requested resource does not support http method 'PUT'."}

但正在正确提供请求GET /api/person

以下是Person Controller

public class PersonController : ApiController
{
    // GET: api/Person/5
    [ResponseType(typeof(Person))]
    public IHttpActionResult GetPerson(int id) { 
        //Served without any issues 
    }

    // PUT: api/Person/5
    [ResponseType(typeof(void))]
    public IHttpActionResult PutPerson(int id, Person person) {
         //Not reaching here!
    }
}

这是 PUT请求,如fiddler

所示
PUT http://localhost:xxxxx/api/person HTTP/1.1
Host: localhost:xxxxx
Connection: keep-alive
Content-Length: 38
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:xxxxx
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36
Content-Type: application/json
DNT: 1
Referer: http://localhost:xxxxx/index.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,ms;q=0.6

{"id":0,"name":"Person 11","age":"11"}

服务器响应

HTTP/1.1 405 Method Not Allowed
Cache-Control: no-cache
Pragma: no-cache
Allow: GET,POST
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcWGFuYWR1XFZTX1Byb2plY3RzXEJhY2tib25lSlNfUmVzdENhbGxzXEJhY2tib25lSlNfUmVzdENhbGxzLldlYlxhcGlccGVyc29u?=
X-Powered-By: ASP.NET
Date: Sat, 16 Aug 2014 09:20:46 GMT
Content-Length: 72

{"message":"The requested resource does not support http method 'PUT'."}

显然,IIS Express拒绝了PUT请求。配置IIS Express(VS 2013)以在项目级别允许PUT和DELETE请求的正确方法是什么。

0 个答案:

没有答案