title: ASP.NET Routing - Route Constraint with database lookup
body: <p>I'm just starting out with C# and ASP.NET and have the following questions.  I am working with code adapted from a couple different tutorials playing with Northwind and have gotten this far.  The list of acceptable categories is currently hard coded in a string but I would like to look up the CategoryName in the database to verify that it exists.  </p>
  
  <p>Obviously the purpose of this is to ensure that users don't just type:<br>
  <em>www.domain.com/Categories/AnyUrlWillWork</em> and return a valid page.</p>
  
  <p>Also does anyone have an tips of how they are dealing with capitalization issues since the routing is case sensitive? For example <em>Categories/beverages</em> should forward to <em>Categories/Beverages</em> ?</p>
  
  <p>Thanks in advance for any assistance, and glad to be joining Stack Overflow.</p>
  
  <pre><code>//Complex contraint class
  public class EchoConstraint : IRouteConstraint
  {
      public readonly string[] ValidMessages = { "Beverages", "Produce", "Confections", "Seafood" };
  
      public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
      {
          string message = values["CategoryName"] as string;
          return ValidMessages.Contains(message);
      }
  }
  
  
  //Routes
  RouteTable.Routes.MapPageRoute(
                          "Category Route",                       // Route name
                          "Categories/{CategoryName}",            // Url pattern
                          "~/ShowProductsInCategory.aspx",        // Physical file
                          true,
                          new RouteValueDictionary            
                              {{"CategoryName", "Beverages"}},         //Sets default value if none is provided in URL
                          new RouteValueDictionary 
                              {{"CategoryName", new EchoConstraint()}}
                             );
  </code></pre>