ASP.NET MVC Programmatically Get a List of Controllers
Asked Answered
M

4

31

In ASP.NET MVC is there a way to enumerate the controllers through code and get their name?

example:

AccountController
HomeController
PersonController

would give me a list such as:

Account, Home, Person
Mass answered 20/7, 2009 at 5:33 Comment(2)
ASP.Net MVC is already doing this, (i.e. it's how the controller names are discovered) if you could leverage the current implementation in MVC it would probably save you some time.Cabinetmaker
that is an excellent question here - how would I do that?Anarchic
T
12

You can reflect through your assembly and find all classes which inherit from type System.Web.MVC.Controller. Here's some sample code that shows how you could do that:

http://mvcsitemap.codeplex.com/WorkItem/View.aspx?WorkItemId=1567

Toyatoyama answered 20/7, 2009 at 5:42 Comment(1)
I ended up using this approach. I also cached the list since it would be reused often. ThanksMass
T
45

Using Jon's suggestion of reflecting through the assembly, here is a snippet you may find useful:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;

public class MvcHelper
{
    private static List<Type> GetSubClasses<T>()
    {
        return Assembly.GetCallingAssembly().GetTypes().Where(
            type => type.IsSubclassOf(typeof(T))).ToList();
    }

    public List<string> GetControllerNames()
    {
        List<string> controllerNames = new List<string>();
        GetSubClasses<Controller>().ForEach(
            type => controllerNames.Add(type.Name));
        return controllerNames;
    }
}
Treatment answered 20/7, 2009 at 10:9 Comment(0)
T
12

You can reflect through your assembly and find all classes which inherit from type System.Web.MVC.Controller. Here's some sample code that shows how you could do that:

http://mvcsitemap.codeplex.com/WorkItem/View.aspx?WorkItemId=1567

Toyatoyama answered 20/7, 2009 at 5:42 Comment(1)
I ended up using this approach. I also cached the list since it would be reused often. ThanksMass
P
0

All who using this post better read this post before: using Assembly.GetCallingAssembly() not returns the calling assembly

The issue is that razor views are acting as independent dynamic assemblies and you don't get the desired assembly.

Yair

Polymorphous answered 11/2, 2012 at 17:32 Comment(1)
I wouldnt call this from a view, I would pass the information from my viewmodel, doing so would mitigate your issue.Underlayer
L
-1

Create the property in every controller and then you get the name like this.

Lentigo answered 20/7, 2009 at 5:37 Comment(2)
This is a good idea, if performance is an issue, then your way is probably the best approach (assuming you don't use reflection to discover the implementations). Otherwise, using reflection to grab the classname of the implementation is the easiest.Cabinetmaker
Rather than paste this into every controller, you can create your own controller class, and make all your controllers be one of this class, and the only thing you put into it that's not in the base controller class is this ViewName propertyHafler

© 2022 - 2024 — McMap. All rights reserved.