0

I am making a PUT request for a PersonController but it is not even reaching the controller. Instead I get following response from Server

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

But a get request GET /api/person is being served properly.

Here is the 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!
    }
}

Here is the PUT request as shown in 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"}

And the Server Response

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'."}

Clearly, the PUT request is rejected by the IIS Express. What is the correct way to configure the IIS Express (VS 2013) to allow PUT and DELETE requests at the project level.

Jatin
  • 4,023
  • 10
  • 60
  • 107
  • First of all, why don't you return the model itself in your ApiController actions? In the other hand, I believe that you should search "Web API enable PUT verb" and it should be enough to find the fix... – Matías Fidemraizer Aug 16 '14 at 09:39
  • I remember that iis express out of the box does not support verbs other than get and post. You have to edit some settings file to turn those verbs on. Google it. – Stephen Chung Aug 16 '14 at 09:42
  • @StephenChung Actually full IIS doesn't have that built-in configured settings. It's about adding them in your web.config. Starting from WebAPI2 VS project template, it creates/adds the required HTTP module entries to the web.config to enable all verbs. – Matías Fidemraizer Aug 16 '14 at 09:43
  • I don't remember it being in web.config. It is in some iis settings file. And only the express version is like this. Full version is not blocked. – Stephen Chung Aug 16 '14 at 09:48
  • @MatíasFidemraizer, StephenChung I had been trying to solve this issue for some time, and I tried all the options that I can find on google and in this forum. The issue turns out to be very silly. I have been making requests like PUT localhost:xxxxx/api/person, without the id. But if I make request like PUT localhost:xxxxx/api/person/11 then the request is mapped correctly to the PUT action method. I am just exploring the Web-API, and the code that I posted for the API controller is generated by Visual Studio. Anyways it seems like PUT is enabled by default, so sorry for bother. – Jatin Aug 16 '14 at 10:14
  • @Jatin What version of WebAPI are you trying-out? Either 1.x or 2.x won't generate that sample code.. – Matías Fidemraizer Aug 16 '14 at 10:18
  • Its Web-API 2. When I right click the Controllers folder (in VS2013) to add a new controller I get this option "Web API 2 Controller with actions, using Entity Framework" along with few others. It generates the code, part of which I posted in my question. – Jatin Aug 16 '14 at 10:30
  • Possible Duplicate - Refer to this answer here http://stackoverflow.com/questions/10906411/asp-net-web-api-put-delete-verbs-not-allowed-iis-8 – satish Aug 16 '14 at 19:46
  • @satish I had already tried the answers in the link that you have posted. Still, I could not get it working and hence asked this question. As said in my earlier comment, the PUT method was already available, its just that I wasn't calling it in a manner expected by the controller action. The issue has now being resolved by properly issuing a PUT request – Jatin Aug 18 '14 at 04:29

0 Answers0