0

I'm trying to implement the pattern that's introduced by this question

The accepted answer provides a strategy where routes have been defined in a separate class. The question I have is exactly how to call that code from Global.asax.

thx

2 Answers 2

1

See another answer from the same question, or more specifically the link provided in that answer.

All that you should have to do is add an instance of ExampleRoute to the RouteCollection in the global.asax.

    public static void RegisterRoutes(RouteCollection routes)
    {
        // ...other routes
        routes.Add(new ExampleRoute());
        // ...more routes
    }
1

Let me show you how can you register route which is specified anywhere in the project as long as the dll file containing lies in the bin folder.

define an interface

public interface IRegiserRoute
{
    void RegisterRoutes(RouteCollection routes);
}

Now say you want to register some route some where far in the jungle. Create a class and inherit it from the above given interface.

public MyBlogRoutes : IRegiserRoute
{
    public void RegisterRoutes(RouteCollection routes)
    {
        //register your stuff here. 

    }

}

Now how to call this route from global.asax file. Load all the types which are inherited from IRegiserRoute (from How to find all the classes which implement a given interface?)

var type = typeof(IRegiserRoute);
var types = AppDomain.CurrentDomain.GetAssemblies().ToList()
.SelectMany(a => a.GetTypes())
.Where(t => type.IsAssignableFrom(t))
select Activator.CreateInstance(t) as IRegiserRoute;

//Now register all your routes anywhere in bin folder.

foreach (var t in types)
{
  t.RegisterRoutes(routes); 
}

I hope this should get you going.

cheers

Parminder

1
  • Late for me to vote this up, but I was searching for something like this and this post have helped me a lot. Alough, is there any alternative for doing this without using reflection, because you scan now all the assemblies and search for the interface. In very large projects, this might become slow
    – Complexity
    Aug 18, 2014 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.