Thursday, June 30, 2016

The requested resource does not support http method 'POST'

I was recently working on developing and testing my ASP.NET Web API layer when I suddenly encountered the following error message while attempting to perform a Post to my Web API controller:

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

Of course, I had designed everything exactly as expected, so I was perplexed as to why this was not working.

I ended up looking at this article, but this did not offer any real clue as to why my system was not working since I had already decorated my Web API methods with the appropriate attributes: http://www.dontpaniclabs.com/blog/post/2013/01/23/that-pesky-requested-resource-does-not-support-http-method-post-error-when-using-mvc-web-api/

Finally, I decided to take a look at my routing configuration in my WebApiConfig.cs file which provided the following default route:


Well, if you look at the default routing rule, you will see that it requires an {action} value in the Url! Therefore, in order to get this to work correctly, I would have to specify a Url such as this instead: http://localhost/api/mymethod/post
Of course, I wanted to avoid using this style of routing in my Web API service layer, since this style is not very "RESTful" so I could either use Attribute-based routing or simply change my WebApiConfig routing to the following:

Once I changed this default routing, I was able to get my Web API Service Layer to behave correctly without this error message!

No comments:

Post a Comment