0

I have an MVC application with some basic routes and routes can be registered dynamiccly (although), that's the id.

I just have basic MVC implementation for the routes:

public class RouteConfig
{
    #region Methods

    /// <summary>
    ///     Registers the routes.
    /// </summary>
    /// <param name="routes">The <see cref="RouteCollection" /> to which the routes will be added.</param>
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
    }

    #endregion
}

Now, I would like something so that it's easy to add routes to the application, but you cannot toutch the source code above for registering routes.

I was thinking about an interface somewhere and that the RouteConfig class will search all the assemblies for classes that implements this interface and than execute the logic defined in there to register the routes, but I do have the feeling that this isn't the correct approach.

I tought about something like this:

Create an interface that defines the way to register routes:

public interface IMvcRouteExtender
{
    #region Methods

    /// <summary>
    ///     Register routes for the application.
    /// </summary>
    /// <param name="routes">The <see cref="RouteCollection"/> that contains all the required routes.</param>
    void RegisterRoutes(RouteCollection routes);

    #endregion
}

Create a class that implements this interface to register the required routes

public class RouteConfig : IMvcRouteExtender
{
    #region IMvcRouteExtender

    /// <summary>
    ///     Registers the routes.
    /// </summary>
    /// <param name="routes">The <see cref="RouteCollection" /> to which the routes will be added.</param>
    public void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute("Default", "pensions/save-and-pension", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
    }

    #endregion
}

But now the issue, how can I make sure that first the IgnoreRoute registration is done, and then calling this class and then registers the rest of the routes?

This all without having strong references between the 2 route configurations?

Has anyone faced something like this and is willing to help me?

I want to avoid tight coupling the RouteConfig to another assembly.

As the SOLID principles say, the system must be open for extension, but closed for modification.

Any help is highly appreciated.

Complexity
  • 5,682
  • 6
  • 41
  • 84

2 Answers2

0

You could easily write the code in your sample like this:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    GlobalConfiguration.Configuration.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional });

    RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    RouteTable.Routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { 
            controller = "Home", 
            action = "Index", 
            id = UrlParameter.Optional });
}

So you don't change code in RouteConfig class. Ofcourse this is not a best practice though...

Edit: Updated question's answer is in this link: How to register routes that have been defined outside Global.asax

Edit2 Please provide those steps, if you want do whatever SOLID princible says...;

  1. Under your solution, create a new project .net library called "CoreRoutes"
  2. Implement routes in there... ( Just abstract new class, let's say "CoreLibraryRoutes" from the routes class. )
  3. Put application start - "searching and registering routes function" in the core app like link below but THE KEY IS; Define it like if a class inherited from CoreLibraryRoutes class... This is a way to do it without performance errors etc.
  4. Now, you can use it without any loss...

I can't make/provide source code because you should try it to learning more... But, if you will not make it whatever you do; Then I can write it for you.

Good luck...

Community
  • 1
  • 1
Lost_In_Library
  • 3,265
  • 6
  • 38
  • 70
  • Sorry but I understood like that, because he/she wrote "but you cannot toutch the source code above for registering routes.".? – Lost_In_Library Aug 18 '14 at 10:16
  • Well, you're free to change it right now to be able to accept routes, but after that, the code will never be touched again. I want to avoid writing touching the route class for registering routes. I was thinking about some plugin system. – Complexity Aug 18 '14 at 10:50
  • Aaa, I see... So you want to build some-kind-of core library. And don't touch core route configs, but extend them from another app etc. - So, please take a look at Parminder's answer : http://stackoverflow.com/questions/8733181/how-to-register-routes-that-have-been-defined-outside-global-asax – Lost_In_Library Aug 18 '14 at 10:53
  • I've updated my question to explain better what I would like to achieve – Complexity Aug 18 '14 at 11:00
  • It's what I needed but it uses reflection which slows down the process a bit. You have any idea about some alternatives? – Complexity Aug 18 '14 at 11:04
  • Sorry but I think, it will not "slowing down the system" if you code it right... Nevermind; Did you look nop commerce plugin system? – Lost_In_Library Aug 18 '14 at 11:06
  • No I haven't but I know for sure that loading types dynamiccly is slower than the "normal" way – Complexity Aug 18 '14 at 11:10
  • I updated my answer as EDIT2*. Please up vote my answer if it's helping you some-how. – Lost_In_Library Aug 18 '14 at 12:37
-1

WebActivator is probably what you're looking for. This will let you create an assembly, with your routes specified within & a method marked to run when the application starts that will load your routes.

Simon Halsey
  • 5,459
  • 1
  • 21
  • 32
  • Thanks for your answer, but it's not quite what I'm looking for. I'm in fact searching for a way to extend it, without using the WebActivator. See it as the following: Add routes to the class without using RouteTables directly, but rather by using a plugin system. – Complexity Aug 18 '14 at 10:51