Is it possible to create routes dynamically in .NET 4?

南笙酒味 提交于 2019-11-29 18:04:45

问题


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 the site read them in programmatically without restarting the application?


回答1:


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.



来源:https://stackoverflow.com/questions/6834112/is-it-possible-to-create-routes-dynamically-in-net-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!