Is it possible to create routes dynamically in .NET 4?
In our application we use the new .NET 4 routing system to route certain requests to other parts of the site. We are only allowed to publish our site code in late evenings which means we have to stay late at work to publish any code changes. We frequently have the need to create custom routes to support legacy links to old content and route them to the new content. These are often needed right away and as our routes are defined in compiled global.asax we reach an impasse when we need these live immediately but cannot do a code push.
Is there a way that we could define routes in some sort of configuration file and have t开发者_如何学JAVAhe site read them in programmatically without restarting the application?
As change of configuration file requires restart of application (and even if would not, routes are registered only on startup, not on every request), I don't see reason why route registration (for start?) could not be in library "just for routing" (Routes.dll)?
I have been using MVCTurbine which supports auto dependency injection/service registration, and route registration. I use class like this for route registration:
public class RouteRegistrator : MvcTurbine.Routing.IRouteRegistrator
{
/// <summary>
/// Registers routes within <see cref="T:System.Web.Routing.RouteCollection"/> for the application.
/// </summary>
/// <param name="routes">The <see cref="T:System.Web.Routing.RouteCollection"/> from the <see cref="P:System.Web.Routing.RouteTable.Routes"/>.</param>
public void Register(System.Web.Routing.RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "MyNamespace.Controllers" }); // Parameter defaults
}
}
This class does not have to be part of webui project, but can be in separate dll. MVCTurbine automatically loads (and calls) all implementations of IRouteRegistrator and IServiceRegistrator which are in libraries in bin folder (not having to be referenced). And, as I know, there is nothing preventing you to add new routes by adding dll which contains new routes in implementation of IRouteRegistrator to bin folder of application. This way, you can add new routes "on the fly", without risking rest of application (new dll is easily removed if something unexpected happens).
If you can't or won't to use MVC Turbine, you can use this concept to "extract" route registration to external dll by passing routes collection from global.asax to dynamically loaded library, containing (only) class with method for route registration.
With this (MVCTurbine or not) as starting point, if needed, you can easily read xml or txt config file into foreach loop for common routes, but that method would be limited to simple routes as it is hard (complicated, but not impossible) to represent any more complicated route configuration in text.
精彩评论