Error in attribute route regex constraint in ASP.NET MVC 6

I add the following route attribute:

[HttpGet]
[Route("add")]
[Route(@"{id:int}/{inn:regex(^[0-9]+$)}/incBalance:range(0,1)/{dateSet:datetime}/{dateNext:datetime}")]
public IActionResult Add(int id, string inn, int incBalance, DateTime dateSet, DateTime dateNext)
{
  ....
}

At execution time error occurs:

An unhandled exception occurred while processing the request.

InvalidOperationException: The following errors occurred with attribute routing information:

Error 1:
For action: 'WebProject.Areas.DAS.Controllers.ReportController.Add'
Error: While processing template 'das/report/[action]/{id:int}/{inn:regex(^[0-9]+$)}/incBalance:range(0,1)/{dateSet:datetime}/{dateNext:datetime}', a replacement value for the token '0-9' could not be found. Available tokens: 'action, area, controller'.

I delete regex(^[0-9]+$) and everything works

Answers


When you use regex in an RouteAttribute you must escape [ and ] characters with [[ and ]], because [ and ] is reserved for the controller parameters (action, controller and area) like in [Route("api/[controller]/[action]")].

Update: Same applies to { and } where you escape it with {{ and }}, but this one is also valid for default routes you setup in app.UseMvc( route => ... ).


Need Your Help

Why can't I add a control the page's header from the an inline ASPX code nugget?

asp.net vb.net .net-4.0 webforms asp.net-4.0

Why can't I add a control the page's header from the an inline ASPX code nugget? It works from the code behind.

How to redirect iframe users?

javascript html iframe web

I have a website: galiro.com/summer.html