
Untitled
By: a guest on
Aug 7th, 2012 | syntax:
None | size: 1.03 KB | hits: 10 | expires: Never
Advice on Large Number of Routes
foreach (string companyName in companyNameList)
{
routes.MapRoute(
name: companyName,
url: companyName,
defaults: new { controller = "CompanyProfile", action = "Index" });
}
routes.MapRoute(
name: "Default",
url: "{company}/{controller}/{action}/{id}",
defaults: new { controller = "CompanyProfile", action = "Index", id = UrlParameter.Optional },
constraints: new { company= new CompanyConstraint(companyNames) });
internal class CompanyConstraint: IRouteConstraint
{
public CompanyConstaint(IList<string> companies)
{
this.companies = companies;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
object company;
if (!values.TryGetValue("company", out company) || company == null)
{
return false;
}
return companies.Contains(company.ToString());
}
}