I have the following route:
context.MapRoute(
"content",
"{page}/{title}",
new { controller = "Server", action = "Index" },
new { page = @"^[AFL][0-9A-Z]{4}$" }
);
This route is used for pages such as:
/A1234
/F6789
/L0123
However it also catches: /Admin which is something I do not want.
I put a temporary solution in place that looks like this:
context.MapRoute(
"content",
"{page}/{title}",
new { controller = "Server", action = "Index" },
new { page = @"^[AFL][0-9][0-9A-Z]{3}$" }
);
This only works because right now all my pages have a 0 in the second digit.
Is there a way I could configure my route to accept A,F or L followed by 4 upper case characters but have it not catch “dmin” ?
Not sure if this is the case but I am thinking the regular expression should not accept “dmin” as it’s in lower case and I only specify A-Z. However when used as an MVC route it does take “dmin”. Does anyone know if ASP MVC internally converts this to all uppercase?
Solution 1: Custom route constraint class
Default route processing ignores case (see code below) when matching URLs that’s why Admin in your case matches as well. All you should do is to write a custom route constraint class that implements
IRouteConstraintinterface and implementMatchmethod appropriately to be case sensitive.Here’s a tutorial to get you started
Solution 2: Custom
RouteclassIf you look at how default
Routeclass processes constraints, this is the code:The last line actually matches provided regular expression route constraints. As you can see it ignores case. So the second possible solution is to write a new
Routeclass that inherits from this defaultRouteclass and overrideProcessConstraintmethod to not ignore case. Everything else can then stay the same.