The Microsoft.AspNetCore.Routing.RoutePatternException
with the message “Invalid route pattern” occurs in ASP.NET Core when the route pattern defined in your application is not valid. This typically happens due to syntax errors, incorrect placeholders, or unsupported characters in the route template.
Here’s a detailed explanation of the issue and how to resolve it:
Common Causes
- Syntax Errors in Route Template
- The route template contains invalid syntax, such as missing or misplaced placeholders.
- Unsupported Characters
- The route template contains characters that are not allowed in route patterns.
- Incorrect Placeholder Usage
- Placeholders (e.g.,
{controller}
,{action}
) are used incorrectly or are missing required constraints.
- Duplicate Placeholders
- The same placeholder is used more than once in the route template.
- Missing or Extra Braces
- The route template has missing
{
or}
characters, or extra braces that are not part of a placeholder.
- Invalid Constraints
- Route constraints (e.g.,
{id:int}
) are used incorrectly or are not supported.
Solutions
1. Check Route Template Syntax
- Ensure the route template follows the correct syntax. A valid route template consists of segments separated by
/
, with optional placeholders enclosed in{}
. Example of a Valid Route Template:
[Route("products/{id:int}")]
public IActionResult GetProduct(int id)
{
// Action logic
}
2. Avoid Unsupported Characters
- Ensure the route template does not contain unsupported characters, such as spaces or special characters (except for
-
,.
, and_
). Invalid Example:
[Route("products/{product name}")] // Space is not allowed
Valid Example:
[Route("products/{product-name}")] // Hyphen is allowed
3. Use Placeholders Correctly
- Ensure placeholders are used correctly and match the action method parameters. Example:
[Route("products/{id}")]
public IActionResult GetProduct(int id) // Parameter name matches placeholder
{
// Action logic
}
4. Avoid Duplicate Placeholders
- Ensure the same placeholder is not used more than once in the route template. Invalid Example:
[Route("products/{id}/{id}")] // Duplicate placeholder
Valid Example:
[Route("products/{id}/details")] // Unique placeholders
5. Check for Missing or Extra Braces
- Ensure all placeholders are properly enclosed in
{}
and there are no extra braces. Invalid Example:
[Route("products/{id")] // Missing closing brace
Valid Example:
[Route("products/{id}")] // Correct braces
6. Use Valid Constraints
- Ensure route constraints are valid and supported. Common constraints include
int
,bool
,datetime
, andminlength
. Example:
[Route("products/{id:int}")] // Constraint ensures id is an integer
public IActionResult GetProduct(int id)
{
// Action logic
}
Debugging Tips
- Use the exception details to identify the exact location of the error in the route template.
- Test the route template in a tool like Route Debugger to validate its correctness.
- Check the ASP.NET Core documentation for supported route template syntax and constraints.
Best Practices
- Use meaningful and consistent route templates.
- Avoid complex route templates; keep them simple and readable.
- Use constraints to enforce parameter types and formats.
- Regularly review and test your route templates to ensure they are valid.
Example of Correct Route Configuration
Controller with Route Attributes
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet("{id:int}")]
public IActionResult GetProduct(int id)
{
// Action logic
}
[HttpGet("search/{name}")]
public IActionResult SearchProduct(string name)
{
// Action logic
}
}
Conventional Routing in Program.cs
(ASP.NET Core 6+)
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Conventional Routing in Startup.cs
(ASP.NET Core 5 or earlier)
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}