0

I am learning MVC 5 and recently my tutor was explaining about setting custom routes. In our custom route we have url:movies/BySelected/{year}/{month}. We need year to be a 4 digit and month to be 2 digit.

So he added some code: @"\d{4}",@"\d{2}"

What does that mean?

Gvs Akhil
  • 2,165
  • 2
  • 16
  • 33

1 Answers1

1

Like the comment said that's a way to add a route constraint. But this doesn't seem to be the right thing unless you plan to have two separate actions that deal with movies/byselected

for example:

movies/byselected/{MovieName} and movies/byselected/{year}/{month}

Here is the warning from the relevant microsoft doc: ``` Warning

Avoid using constraints for input validation, because doing so means that invalid input will result in a 404 (Not Found) instead of a 400 with an appropriate error message. Route constraints should be used to disambiguate between similar routes, not to validate the inputs for a particular route. ```

Also - there is a much nicer syntax for constraints:

In MVC5 you can use attribute routes and then you can use the inline constraint syntax, but at this point in time you should probably look at MVC core. You can spec:

"movies/BySelected/{year:length(4)}/{month:length(2)}
Yishai Galatzer
  • 8,791
  • 2
  • 32
  • 41
  • Ya thanks a lot and as it is starting of tutorial he explained the first method nd now he s using attribute routes ☺️..... – Gvs Akhil Jul 24 '18 at 18:46