我添加以下路由属性:
[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)
{
....
}
在执行时发生错误:
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'.
我删除regex(^[0-9]+$)
一切正常
当您在RouteAttribute
中使用正则表达式时,您必须转义[
并用 [[
和 ]]
]
字符,因为[
和]
是为控制器参数(动作、控制器和区域)保留的,就像在 [Route("api/[controller]/[action]")]
中一样。
更新:这同样适用于使用 {{
和 }}
转义的{
和}
,但这也适用于您在 app.UseMvc( route => ... )
中设置的默认路由。