AttributeRouting icon indicating copy to clipboard operation
AttributeRouting copied to clipboard

URL generation parameter format

Open uksus70 opened this issue 11 years ago • 1 comments

Hi.

Is it possible to configure AttributeRouting so that some parameter would be formatted using specific format? For example, I have an action:

        [GET("{year:int:length(4)}-{month:range(1, 12)}")]
        public ActionResult Days(int year, int month)

I want the month parameter to be always two-digit. How to achieve that with AttributeRouting?

uksus70 avatar Oct 10 '13 09:10 uksus70

If I understand your question correct, you want to ensure that the month param is always two digits. This could be accomplished using regex:

[GET("{year:int:length(4)}-{month(^\d{2}$)}")]
public ActionResult Days(int year, int month)

To make it a bit more readable to humans:

[GET("{year:int:length(4)}-{month}")]
[RegexRouteConstraint("month", @"^\d{2}$")]
public ActionResult Days(int year, int month)

Again, to add a little more "validation", your {month} regex should be:

0[1-9]|1[0-2]

This will force your month route to match one of the following: 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12.

Kordonme avatar Feb 07 '14 14:02 Kordonme