I am trying to write a series of routing rules to account for parameters used to filter geographic locations and categories. Before I past them I am going to describe what they should do and what they are actually doing.
These rules should result in a URLs like example.com serving ~/Default.aspx, example.com/ca/ serving ~/local/Default.aspx, and example.com/category/ serving ~/category/Default.aspx.
What the rules are actually doing is matching a location or a category depending on what order they are listed in the routing rules. This should not be the case because the location rules have a two character length constraint, so anything exceeding two characters should not match and the next rule should be processed. Unfortunately, these rules are routing /category/ to the local page even thought the length of the category exceeds two characters. Then if I try running the category rule before the location it of course tries to serve the location from the category page because there is no length constraint on categories.
The rules are as follows:
routes.MapPageRoute("home", "", "~/Default.aspx");
routes.MapPageRoute("local/state/city/cat/scat", "{state}-{city}/{catslug}/{scatslug}", "~/local/Default.aspx", false, new RouteValueDictionary()
{
{
"state",
(object) "[a-z]{2}"
},
{
"city",
(object) "\\w+"
}
});
routes.MapPageRoute("local/state/city/cat", "{state}-{city}/{catslug}", "~/local/Default.aspx", false, new RouteValueDictionary()
{
{
"state",
(object) "[a-z]{2}"
},
{
"city",
(object) "\\w+"
}
});
routes.MapPageRoute("local/state/city", "{state}-{city}", "~/local/Default.aspx", false, new RouteValueDictionary()
{
{
"state",
(object) "[a-z]{2}"
},
{
"city",
(object) "\\w+"
}
});
routes.MapPageRoute("local/state/cat/scat", "{state}/{catslug}/{scatslug}", "~/local/Default.aspx", false, new RouteValueDictionary()
{
{
"state",
(object) "[a-z]{2}"
}
});
routes.MapPageRoute("local/state/cat", "{state}/{catslug}", "~/local/Default.aspx", false, new RouteValueDictionary()
{
{
"state",
(object) "[a-z]{2}"
}
});
routes.MapPageRoute("local/state", "{state}", "~/local/Default.aspx", false, new RouteValueDictionary()
{
{
"state",
(object) "[a-z]{2}"
}
});
routes.MapPageRoute("category", "{catslug}", "~/category/Default.aspx", false, new RouteValueDictionary()
{
{
"catslug",
(object) "[a-z]"
}
});
routes.MapPageRoute("category/subcategory", "{catslug}/{scatslug}", "~/category/Default.aspx", false, new RouteValueDictionary()
{
{
"catslug",
(object) "[a-z]"
},
{
"scatslug",
(object) "[a-z]"
}
});
My question is of course how do I change these rules so that only parameters that are two characters long are routed to the location page and everything else is routed to the category page?
That is the problem, but I don't know why. The route is enabled for the category page and not disabled for it either, but if those routes are listed after the location routes then they are still routed to the local page.
IT seems like the problem is that the category slug is being treated like a location slug even though it is longer than 2 characters despite the fact that I clearly have a 2 character constraint on the first slug of the locations.
Since I am totally stumped here I was thinking of maybe changing the routes back to where they were before. Before they would begin with "local" like "local/{state}" being routed to "~/local/Default.aspx" and then using URL rewriting in my web.config to rewrite /{state} to local/{state}
Hi @PostAlmostAnything ,
According to your description, could you post your request url to us? As far as I think,one reason is your request url is match local route and the other is your local route rule's constraint not working.
If your request url don't match local route,it not possible route to local's page.
Best regards,
yijing Sun
Some of the URLs are things like example.com/mycategoryname/mysubcatgoryname which should not match a route with the [a-z]{2} constraint because mycategoryname is more than 2 characters.
Hi @PostAlmostAnything ,
I think you don't use routes.MapPageRoute correctly. According your urls,your MapPageRoute 's format is:
The routeUrl is the URL pattern for the route and the function you don't match.
You could check your function's parameter.
More details,you could refer to below article:
https://learn.microsoft.com/en-us/dotnet/api/system.web.routing.routecollection.mappageroute?view=netframework-4.8#System_Web_Routing_RouteCollection_MapPageRoute_System_String_System_String_System_String_System_Boolean_System_Web_Routing_RouteValueDictionary_
Best regards,
Yijing Sun
That last comment makes no sense to me at all. I know that the match is not working for some reason. Do you have an alternative constraint you can suggest for exactly two letters?
I tried changing the state constraint to "state", @"^([a-z]{2})$"
and the catslug constraint to "catslug", @"^[a-z]{3,}" but the problem persists even though state is always 2 characters and catslug is always more than 3.
Hi @PostAlmostAnything ,
I think what the function you use is very important. According to your codes,your function is this:
The RouteValueDictionary is default values for the route parameters.
Your function must be this:
The first RouteValueDictionary is the default values for the route. The second RouteValueDictionary is constraints that a URL request must meet in order to be processed as this route.
Best regards,
Yijing Sun
Could you show me what a rule like that would look like? Like if you were to write a rule for just the state slug the way you describe?
Hi @PostAlmostAnything ,
You could refer the article:
The routeName is the name of the route. The routeUrl is the URL pattern for the route. The physicalFile is the physical URL for the route.
The checkPhysicalUrlAccess is a value that indicates whether ASP.NET should validate that the user has authority to access the physical URL (the route URL is always checked).
The two RouteValueDictionary meanings I have talked.
Best regards,
Yijing Sun
Sign in to comment
Activity