Reputation:
What is the purpose of :long
part in the following route ?
[HttpGet("api/{users:long}")]
public async Task<IActionResult> Get([FromRoute] long userId)
{
...
}
I've seen this on a Controller endpoint, but never used it, so what does it do ?
Upvotes: 0
Views: 84
Reputation: 58863
Those are called inline route constraints; you can check an article on them here: https://mariusschulz.com/blog/inline-route-constraints-in-asp-net-core-mvc.
It makes the endpoint check that the route value is indeed a long
, otherwise it won't select that action to run at all.
It shouldn't be used for validation IMO, Marius shows a good example where these constraints can be used:
public class MessagesController : ApiController
{
[Route("messages/{messageId:int}")]
public Message Get(int messageId)
{
// ...
}
[Route("messages/{messageId:guid}")]
public Message Get(Guid messageId)
{
// ...
}
}
Depending on the type of the route value, a different action can be selected.
Upvotes: 1