question

PostAlmostAnything-1364 avatar image
0 Votes"
PostAlmostAnything-1364 asked YijingSun-MSFT commented

ASP.Net Routing Rules Fail to Honor Character Lenth Constraints

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?

dotnet-aspnet-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @PostAlmostAnything-1364 ,
As far as I think, you could use Regular Expression to set the route rules.
Just like this:

  routes.MapRoute(
         constraints: new { id = @"^([-]*[a-zA-Z0-9]*-[a-zA-Z0-9]*[-]*)+$" }
     );

Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PostAlmostAnything-1364 avatar image
0 Votes"
PostAlmostAnything-1364 answered YijingSun-MSFT commented

How would your suggestion help my current rules distinguish between a two character parameter and anything else?

I just need to change this so that if the parameter is more than 2 characters that it routes to a different place.

· 12
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @PostAlmostAnything-1364 ,
I think I know your problem. When you should access category route, it still route to the local page.In some situations, routing does not handle a request despite being enabled; for example, if a physical file exists which matches the URL pattern, or if routing has been explicitly disabled for a pattern. I think you could check.
Best regards,
Yijing Sun

0 Votes 0 ·

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.

0 Votes 0 ·

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.

0 Votes 0 ·

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}

0 Votes 0 ·
YijingSun-MSFT avatar image YijingSun-MSFT PostAlmostAnything-1364 ·

Hi @PostAlmostAnything-1364 ,
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

0 Votes 0 ·
Show more comments