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?
-
2Did you try adding explicitly attribute Get on the actions? ` [System.Web.Http.HttpGet] public async Task<string> Lookup(string id) { // do lookup return ""; }`Selvirrr– Selvirrr2018年08月28日 00:22:14 +00:00Commented 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.Teoman shipahi– Teoman shipahi2018年08月28日 01:00:47 +00:00Commented Aug 28, 2018 at 1:00
1 Answer 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.
2 Comments
Explore related questions
See similar questions with these tags.