Pacman
Pacman

Reputation: 2245

WebAPI - how do I "overload" the GET method?

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

if the user calls:

GET /users/1234

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

Answers (2)

JSilvanus
JSilvanus

Reputation: 145

See here: https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

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

Max Bündchen
Max Bündchen

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

Related Questions