0

I have following web api controller:

public class ProductController : ProductControllerBase
{
 public async Task<string> Lookup(string id)
{
   // do lookup
}

public async Task<string> Search(string keywords)
{
    // do search
}
}

Sample requests to this api:
http://localhost/api/product/Lookup?provider=amazon&id=B07CSPSMQY
http://localhost/api/product/Lookup?provider=walmart&id=4132478AB
http://localhost/api/product/Lookup?provider=ebay&id=EWRHNFKASDN231

I am fetching provider in base class, because I use it for IoC purposes:

public ProductControllerBase()
{
    string provider = HttpContext.Current.Request.QueryString["provider"];
    // resolve search provider depending on parameter
    SetController(provider);
}

In my WebApiConfig I have following setup:

config.Routes.MapHttpRoute(
    "ProductControllerLookup",
    "api/product/Lookup/{provider}/{id}",
    new
    {

    }
);

but once I run URLs above, I keep getting following error:

{"Message":"The requested resource does not support http method 'GET'."}

What I want is to fetch parameters other than "provider", since I did not want to use it for all methods.

How can I configure this one?

2
  • 2
    Did you try adding explicitly attribute Get on the actions? ` [System.Web.Http.HttpGet] public async Task<string> Lookup(string id) { // do lookup return ""; }` Commented Aug 28, 2018 at 0:22
  • If I only send id parameter but not provider it hits the method. But if I send both id and provider I got error above. Commented Aug 28, 2018 at 1:00

1 Answer 1

1

I've tested the code just with adding [HttpGet] attribute, and it seems to be ok, here is my WebApiConfig

public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
           "ProductControllerLookup",
           "api/product/Lookup/{provider}/{id}",
            new {}
        );
    }

And here is the method:

[System.Web.Http.HttpGet]
    public async Task<string> Lookup(string id)
    {
        var temp = HttpContext.Current.Request.QueryString["provider"];
        // do lookup
        return "";
    }

And I'm using your url's and all of them are hitting the method and id and provider are filled.

Sign up to request clarification or add additional context in comments.

2 Comments

OK, thanks for your test. Looks like order of Routes setting was not correct. When I move ProductControllerLookup below to DefaultApi it started to work.
And yes, seems like adding System.Web.Http.HttpGet worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.