AspNetCore.Docs
AspNetCore.Docs copied to clipboard
More info needed re: route constraint vs. no route constraint
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.
- ID: 8ee6ad2f-1667-8d5c-1f36-9d0511e3720f
- Version Independent ID: 7838ccec-c8e8-9baa-2b3e-220b2af5fc58
- Content: Routing to controller actions in ASP.NET Core
- Content Source: aspnetcore/mvc/controllers/routing.md
- Product: aspnet-core
- Technology: aspnetcore-mvc
- GitHub Login: @Rick-Anderson
- Microsoft Alias: riande
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 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.
Thank you @Rick-Anderson , I appreciate it!
See https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-6.0#route-constraints