Hi. 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:
http://mywebsite.com/api/users/5
it must execute the first action and if I request
http://mywebsite.com/api/users/abcde
it must execute the second one... Any ideas? Thanks...