ASP.NET MVC 6 中的属性路由正则表达式约束错误

Kol*_*Net 3 c# regex asp.net-core-mvc asp.net-core asp.net-core-1.0

我添加以下路由属性:

[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)
{
  ....
}
Run Code Online (Sandbox Code Playgroud)

在执行时出现错误:

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'.
Run Code Online (Sandbox Code Playgroud)

我删除regex(^[0-9]+$),一切正常

Tse*_*eng 5

当您在 an 中使用正则表达式时,RouteAttribute您必须使用and转义[]字符,因为and是为控制器参数(动作、控制器和区域)保留的,如 in 。[[]][][Route("api/[controller]/[action]")]

更新:同样适用于{}在那里你逃避它{{}},但是这一次也同样适用于您在设置默认路由app.UseMvc( route => ... )