AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

More info needed re: route constraint vs. no route constraint

Open smarts opened this issue 3 years ago • 3 comments

This page talks about route constraints, but does not highlight the difference in behavior [for non-compliant values] when using a route constraint vs. not using a route constraint. Simple example:

[HttpGet("resources/{id}")]
public Task<ActionResult> GetResourceById([FromRoute] Guid id)
{
  return Ok();
}

vs.

[HttpGet("resources/{id:guid}")] // ← Notice route constraint
// …same code as above

When using a non-GUID value (ex: "foo") in the URL these two actions have varying responses.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

smarts avatar Sep 02 '22 23:09 smarts

Hello! Thanks for contacting us. This sounds like a general question about using ASP.NET Core. While we try to look at and respond to all issues, for questions like this we recommend posting to a community support group like Stack Overflow with the asp.net-core tag.

Potential bugs in the ASP.NET Core framework and product feedback can be reported at dotnet/aspnetcore issues (include full repro steps and cross-link to a repro project in GH when appropriate). For Visual Studio, use the Report a Problem gesture or Suggest a Feature gesture within VS, which open internal issues for the VS team. For Visual Studio Code, you can open an issue at microsoft/vscode issues.

Rick-Anderson avatar Sep 03 '22 01:09 Rick-Anderson

@Rick-Anderson can you please re-open this? This isn't a question at all, it's a request to update the documentation to highlight a difference in behavior of 2 options that are seemingly the same.

smarts avatar Sep 09 '22 09:09 smarts

Thank you @Rick-Anderson , I appreciate it!

smarts avatar Sep 15 '22 04:09 smarts

See https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-6.0#route-constraints

Rick-Anderson avatar Sep 30 '22 03:09 Rick-Anderson