ASP.NET MVC is even more configurable than you think!
Routes are registered in the Application Start of an MVC application, but there is no reason that they have to be hard coded in the Global.asax. By simply reading routes out of the Web.config you provide a way to control routing without having to redeploy code, allowing you to enable or disable website functionality on the fly.
I can't take credit for this idea, my implementation is an enhancement of Fredrik Normén's MvcRouteHandler that adds a few things that were missing:
Routes are registered in the Application Start of an MVC application, but there is no reason that they have to be hard coded in the Global.asax. By simply reading routes out of the Web.config you provide a way to control routing without having to redeploy code, allowing you to enable or disable website functionality on the fly.
I can't take credit for this idea, my implementation is an enhancement of Fredrik Normén's MvcRouteHandler that adds a few things that were missing:
- Optional Parameters
- Typed Constraints
- Data Tokens
- An MVC3 Library
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); var routeConfigManager = new RouteManager(); routeConfigManager.RegisterRoutes(routes); }Example Web.config
<configuration> <configSections> <section name="routeTable" type="MvcRouteConfig.RouteSection" /> </configSections> <routeTable> <routes> <add name="Default" url="{controller}/{action}/{id}"> <defaults controller="Home" action="Index" id="Optional" /> <constraints> <add name="custom" type="MvcApplication.CustomConstraint, MvcApplication"> <params value="Hello world!" /> </add> </constraints> </add> </routes> </routeTable>
No comments:
Post a Comment