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!
{"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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void Register(HttpConfiguration config) | |
{ | |
// Web API attribute routes | |
config.MapHttpAttributeRoutes(); | |
//Map default routes | |
config.Routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{version}/{controller}/{action}/{id}", | |
defaults: new { controller = "Home", version = 1, action = "Get", id = RouteParameter.Optional } | |
); | |
} |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void Register(HttpConfiguration config) | |
{ | |
// Web API attribute routes | |
config.MapHttpAttributeRoutes(); | |
//Map default routes | |
config.Routes.MapHttpRoute( | |
name: "DefaultRESTfulApi", | |
routeTemplate: "api/{version}/{controller}/{id}", | |
defaults: new { controller = "Home", version = 1, id = RouteParameter.Optional } | |
); | |
} |