I have an ASP .Net Core 1.1 MVC Web API. How can I have a string route constraint in a controller action?
I have the following two actions:
/ GET: api/Users/5
[HttpGet("{id:int}")]
[Authorize]
public async Task<IActionResult> GetUser([FromRoute] int id)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
User user = await _context.User.SingleOrDefaultAsync(m => m.UserId == id);
if (user == null)
return NotFound();
return Ok(user);
}
// GET: api/Users/abcde12345
[HttpGet("{nameIdentifier:string}")]
[Authorize]
public async Task<IActionResult> GetUserByNameIdentifier([FromRoute] string nameIdentifier)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
User user = await _context.User.SingleOrDefaultAsync(m => m.NameIdentifier == nameIdentifier);
if (user == null)
return NotFound();
return Ok(user);
}
The first one works but the second doesn't - .Net doesn't like the "string" constraint. So basically, id I execute an HTTPGET request again:
it must execute the first action and if I request
it must execute the second one... Any ideas? Thanks...