Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 7th, 2012  |  syntax: None  |  size: 1.03 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Advice on Large Number of Routes
  2. foreach (string companyName in companyNameList)
  3. {
  4.     routes.MapRoute(
  5.         name: companyName,
  6.         url: companyName,
  7.         defaults: new { controller = "CompanyProfile", action = "Index" });
  8. }
  9.        
  10. routes.MapRoute(
  11.             name: "Default",
  12.             url: "{company}/{controller}/{action}/{id}",
  13.             defaults: new { controller = "CompanyProfile", action = "Index", id = UrlParameter.Optional },
  14.             constraints: new { company= new CompanyConstraint(companyNames) });
  15.        
  16. internal class CompanyConstraint: IRouteConstraint
  17. {
  18.     public CompanyConstaint(IList<string> companies)
  19.     {
  20.         this.companies = companies;
  21.     }
  22.  
  23.     public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
  24.     {
  25.         object company;
  26.         if (!values.TryGetValue("company", out company) || company == null)
  27.         {
  28.              return false;
  29.         }
  30.  
  31.         return companies.Contains(company.ToString());
  32.     }
  33. }