Reputation: 2245
So I am new to ASP.NET webAPI and I created a controller called: UsersController, which exposes the 4 CRUD methods. If the user calls:
GET/Users
this will use the default Get method
public IEnumerable Get()
if the user calls:
GET /users/1234
this in turn will call:
public string Get(int id)
BUT... what if I need something like:
GET / Users/Males
GET /Users/Tall
how do I override/overload the GET method ?
Upvotes: 4
Views: 2279
Reputation: 145
Route Constraints Route constraints let you restrict how the parameters in the route template are matched. The general syntax is "{parameter:constraint}". For example:
[Route("users/{id:int}")]
public User GetUserById(int id) { ... }
Route("users/{name}")]
public User GetUserByName(string name) { ... }
Upvotes: 0
Reputation: 1362
Use RouteAttribute.
In your Api Controller:
public IEnumerable Get()
{
}
public string Get(int id)
{
}
[Route("/Users/Tall")]
public IEnumerable GetTall()
{
}
More info about: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2.
Upvotes: 1