2

I think I am doing this constraint wrong. I want the year to be 4 digits, the month to be 2 digits, and the month to range from 1 to 12:

[Route("projects/released/{year:regex(\\d{4})}/{month:regex(\\d{2}):range(1,12)}")]

I get the following error in rider:

Route parameter constraint 'regex(\d{2' not resolved

I'm running .NET 6 ASP.NET Core MVC

1 Answer 1

2

Use the following:

[Route("projects/released/{year:regex(^\\d{{4}}$)}/{month:regex(^\\d{{2}}$):range(1,12)}")]
public IActionResult Index(string year, string month)
{
    // your code...
}

See detailed description in the documentation: Regular expressions in constraints

Pay attention: to escape routing parameter delimiter characters {, }, [, ], double the characters in the expression, for example, {{, }}, [[, ]].

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

1 Comment

Ok I see. So the beginning ^ and ending $ contains the inline constraint. Thank you!

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.