3

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

1 Answer 1

5

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 => ... ).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.