Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 660
Chev Avatar asked Jul 26 '11 17:07

Chev


People also ask

What is dynamic routing in MVC?

What is Dynamic Routing? MVC Routing operates on the basis that a request is handled by Controllers and Actions in predefined routes. Dynamic Routing is where a request maps to a Page (TreeNode), and that Page's settings (Template, Page Type) determines how the request should be handled (which Controller/Action/View).

What is Route collection in MVC?

The RouteCollection class provides methods that enable you to manage a collection of objects that derive from the RouteBase class. Typically, you will use the static Routes property of the RouteTable class to retrieve a RouteCollection object. The Routes property stores all the routes for an ASP.NET application.

What is a route in C#?

Routing is used to map requests to route handlers. Routes are configured when the application starts up, and can extract values from the URL that will be used for request processing.

What is route in asp net?

In this article The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. By the end of this tutorial, you will understand how the standard route table maps requests to controller actions.


1 Answers

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.

like image 111
Goran Obradovic Avatar answered Oct 05 '22 22:10

Goran Obradovic