是否可以在 .NET 4 中动态创建路由? [英] Is it possible to create routes dynamically in .NET 4?

查看:20
本文介绍了是否可以在 .NET 4 中动态创建路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序中,我们使用新的 .NET 4 路由系统将某些请求路由到站点的其他部分.我们只允许在深夜发布我们的站点代码,这意味着我们必须加班到很晚才能发布任何代码更改.我们经常需要创建自定义路由来支持旧内容的旧链接并将它们路由到新内容.这些通常是立即需要的,因为我们的路由是在已编译的 global.asax 中定义的,当我们需要立即使用这些但无法执行代码推送时,我们会陷入僵局.

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?

推荐答案

由于配置文件的更改需要重新启动应用程序(即使不会,路由仅在启动时注册,而不是在每个请求时注册),我不看到为什么路由注册(用于开始?)不能在库中仅用于路由"(Routes.dll)?

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)?

我一直在使用 MVCTurbine,它支持自动依赖注入/服务注册和路由注册.我使用这样的类进行路由注册:

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
        }
    }

这个类不必是 webui 项目的一部分,但可以在单独的 dll 中.MVCTurbine 自动加载(并调用)所有 IRouteRegistrator 和 IServiceRegistrator 的实现,它们位于 bin 文件夹中的库中(不必被引用).而且,据我所知,没有什么可以阻止您通过将包含 IRouteRegistrator 实现中的新路由的 dll 添加到应用程序的 bin 文件夹来添加新路由.通过这种方式,您可以即时"添加新路由,而不会影响应用程序的其余部分(如果发生意外情况,可以轻松删除新的 dll).

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).

如果你不能或不会使用 MVC Turbine,你可以使用这个概念通过将路由集合从 global.asax 传递到动态加载的库,包含(仅)类与路由注册方法.

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.

以此(MVCTurbine 与否)为起点,如果需要,您可以轻松地将 xml 或 txt 配置文件读入 foreach 循环以获取常见路由,但该方法仅限于简单路由,因为它很难(复杂,但并非不可能)以文本形式表示任何更复杂的路由配置.

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.

这篇关于是否可以在 .NET 4 中动态创建路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆