ASP.NET Web Api: The requested resource does not support http method 'GET'
asp.netasp.net Web-Apiasp.net Problem Overview
I've got the following action on an ApiController:
public string Something()
{
return "value";
}
And I've configured my routes as follows:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
In the beta, this worked just fine, but I just updated to the latest Release Candidate and now I'm seeing errors on calls like this:
> The requested resource does not support http method 'GET'.
Why doesn't this work anymore?
(I suppose I could get rid of {action} and just make a ton of controllers, but that feels messy.)
asp.net Solutions
Solution 1 - asp.net
If you have not configured any HttpMethod on your action in controller, it is assumed to be only HttpPost in RC. In Beta, it is assumed to support all methods - GET, PUT, POST and Delete. This is a small change from beta to RC. You could easily decore more than one httpmethod on your action with [AcceptVerbs("GET", "POST")].
Solution 2 - asp.net
All above information is correct, I'd also like to point out that the [AcceptVerbs()]
annotation exists in both the System.Web.Mvc and System.Web.Http namespaces.
You want to use the System.Web.Http if it's a Web API controller.
Solution 3 - asp.net
Although this isn't an answer to the OP, I had the exact same error from a completely different root cause; so in case this helps anybody else...
The problem for me was an incorrectly named method parameter which caused WebAPI to route the request unexpectedly. I have the following methods in my ProgrammesController:
[HttpGet]
public Programme GetProgrammeById(int id)
{
...
}
[HttpDelete]
public bool DeleteProgramme(int programmeId)
{
...
}
DELETE requests to .../api/programmes/3 were not getting routed to DeleteProgramme as I expected, but to GetProgrammeById, because DeleteProgramme didn't have a parameter name of id. GetProgrammeById was then of course rejecting the DELETE as it is marked as only accepting GETs.
So the fix was simple:
[HttpDelete]
public bool DeleteProgramme(int id)
{
...
}
And all is well. Silly mistake really but hard to debug.
Solution 4 - asp.net
If you are decorating your method with HttpGet
, add the following using
at the top of the controller:
using System.Web.Http;
If you are using System.Web.Mvc
, then this problem can occur.
Solution 5 - asp.net
This is certainly a change from Beta to RC. In the example provided in the question, you now need to decorate your action with [HttpGet] or [AcceptVerbs("GET")].
This causes a problem if you want to mix verb based actions (i.e. "GetSomething", "PostSomething") with non verb based actions. If you try to use the attributes above, it will cause a conflict with any verb based action in your controller. One way to get arount that would be to define separate routes for each verb, and set the default action to the name of the verb. This approach can be used for defining child resources in your API. For example, the following code supports: "/resource/id/children" where id and children are optional.
context.Routes.MapHttpRoute(
name: "Api_Get",
routeTemplate: "{controller}/{id}/{action}",
defaults: new { id = RouteParameter.Optional, action = "Get" },
constraints: new { httpMethod = new HttpMethodConstraint("GET") }
);
context.Routes.MapHttpRoute(
name: "Api_Post",
routeTemplate: "{controller}/{id}/{action}",
defaults: new { id = RouteParameter.Optional, action = "Post" },
constraints: new { httpMethod = new HttpMethodConstraint("POST") }
);
Hopefully future versions of Web API will have better support for this scenario. There is currently an issue logged on the aspnetwebstack codeplex project, http://aspnetwebstack.codeplex.com/workitem/184. If this is something you would like to see, please vote on the issue.
Solution 6 - asp.net
Have the same Setup as OP. One controller with many actions... less "messy" :-)
In my case i forgot the "[HttpGet]" when adding a new action.
[HttpGet]
public IEnumerable<string> TestApiCall()
{
return new string[] { "aa", "bb" };
}
Solution 7 - asp.net
Same problem as above, but vastly different root. For me, it was that I was hitting an endpoint with an https rewrite rule. Hitting it on http caused the error, worked as expected with https.
Solution 8 - asp.net
Replace the following code in this path
Path :
App_Start => WebApiConfig.cs
Code:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}/{Param}",
defaults: new { id = RouteParameter.Optional,
Param = RouteParameter.Optional }
);
Solution 9 - asp.net
I don't know if this can be related to the OP's post but I was missing the [HttpGet] annotation and that was what was causing the error, as stated by @dinesh_ravva methods are assumed to be HttpPost by default.
Solution 10 - asp.net
My issue was as simple as having a null reference that didn't show up in the returned message, I had to debug my API to see it.
Solution 11 - asp.net
I got this error when running a query without SSL.
Simply changing the URL scheme of my request from HTTP to HTTPS fixed it.