I have a pair of GET and POST Web API methods that work fine. I have another pair that seem to be set up the exact same way, and yet the GET method refuses to accept calls. All attempts to do so end with the msg, "The requested resource does not support http method 'GET'."
Yet (again) this GET method is set up and decorated the same as the working one.
Here is the working one (both the unlabeled "GET" (first method) and the labeled "POST" are successfully entered when called):
[RoutePrefix("api/pricecompliance")]
public class PriceComplianceController : ApiController
{
[Route("{unit}/{begindate}")]
public HttpResponseMessage Get(string unit, string begindate)
{
. . .
[Route("{unit}/{begindate}")]
[HttpPost]
public void Post(string unit, string begindate)
{
. . .
...and here is the pair that seem almost identical (different name, of course, and one more parameter):
[RoutePrefix("api/produceusage")]
public class ProduceUsageController : ApiController
{
[Route("{unit}/{begindate}/{enddate}")]
public HttpResponseMessage Get(string unit, string beginRange, string
endRange)
{
. . .
[Route("{unit}/{begindate}/{enddate}")]
[HttpPost]
public void Post(string unit, string begindate, string enddate)
{
. . .
As mentioned, With the non-working second block code, when I try to call the "GET", I get:
The requested resource does not support http method 'GET'.
I do have attribute routing set up like so:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
Why might it be that GET is not supported in the latter case?
Both when I try to call GET by clicking a link with the URL and when I try to call it from Postman with "http://localhost:52194/api/produceusage/gramps/201312/201412", I get that same "GET not supported" jazz.
It turned out that I had to change this:
public HttpResponseMessage Get(string unit, string beginRange, string endRange)
...to this:
public HttpResponseMessage Get(string unit, string begindate, string enddate)
...so that the parameter names exactly matched those in the route:
[Route("{unit}/{begindate}/{enddate}")]
Why that matters I don't know, but it does.