Our MVC3 routing engine has a couple entries which have a constraint which involves a database lookup to evaluate. For example:
routes.MapRoute(
"Product",
"{manufacturer}/{partNumber}",
new { controller = "Product", action = "Details", manufacturer = "" },
new { manufacturer = new ManufacturerConstraint() }
);
routes.MapRoute(
"Store",
"{store}/{action}",
new { controller = "Store", action = "Index" },
new { store = new StoreConstraint() }
);
where ManufacturererConstraint() involves a database lookup and StoreConstraint() does not.
We’re using RouteUrl to generate a link similar to:
RouteUrl("Product", new { manufacturer = product.Brand, partNumber = product.PartNumber });
Three questions from this:
- Does our usage cause a database lookup?
- If I generated a route for the “Store” route, would that also generate a lookup as it tests it against all routes? Or would it only do the one test for the specified route?
- If it does hit the database in this usage, is there a way to use
RouteUrlthat wouldn’t?
Yes, if the constraint is setup to work on UrlGeneration.
Url.RouteUrlruns all constraints, just likeUrl.Action. The only difference is that you are explicitly saying which route you would like to use, instead of testing each route until one matches.I think I answered this above.
Setup the constraint so it doesn’t run on UrlGeneration (using the
routeDirectionparameter). Personally, I’d cache the lookup data instead.