I want to have to GET methods on my api, one with the Route with path parameters:
api/people/{personId}
and one with the Route with query parameters:
api/people?text=something
but if i put this code:
// GET: api/people/{personId}
[Route("api/people/{personId}")]
[HttpGet]
public HttpResponseMessage Get(long personId)
{
}
// GET: api/people?text=something
[Route("api/people")]
[HttpGet]
public HttpResponseMessage Get(string text)
{
}
And then try to open /api/people/1 it says wrong format and when I try to open /api/people?text=something it works.
I only have the default route defined:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
How can I have them both working? Define that if it's a path parameter go to the first one and if it's a query parameter go to second on?
I just put this into Visual Studio 2015 using Web API 2 and it works fine. I did have to add the following lines (one for each controller method).
return Request.CreateResponse(HttpStatusCode.OK);
I also blew away the Default Route Config. The following urls work just fine
http://localhost:64377/api/people/1
http://localhost:64377/api/people?text=Hello
I hit both respective methods in my controller when I put these URLs in IE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With