ASP.Net Routing Rules Fail to Honor Character Lenth Constraints

PostAlmostAnything 1 Reputation point
2021-06-20T22:44:51.487+00:00

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?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,251 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,066 Reputation points
    2021-06-21T08:37:39.577+00:00

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

    0 comments No comments

  2. PostAlmostAnything 1 Reputation point
    2021-06-22T21:49:11.01+00:00

    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.