4

I need to get all areas that has been registered as list and all of their controllers as a sub list and the same thing for actions. something like this:

AdminArea
   HomeController
     Index
     Add
     ...
   OtherController
   ...
AnotherArea
   HomeController
      Index
      ...
...

I have checked the answer of this question, and this one, but they are not what i'm looking for. The first one return all the routes that has been registered and the second one return all controllers at once.

Update

Ok, with the below code i can get all the areas and with the answer of the second question i can get all the controllers, but i can't figure it out each controller belong to what area.

var areaNames = RouteTable.Routes.OfType<Route>()
            .Where(d => d.DataTokens != null && d.DataTokens.ContainsKey("area"))
            .Select(r => r.DataTokens["area"]).ToList()
            .Distinct().ToList();
Community
  • 1
  • 1
kamal hamidi
  • 665
  • 1
  • 6
  • 16
  • 2
    Why are those questions not what you're looking for? Help us know why they don't work for you. – krillgar Feb 05 '16 at 13:50
  • I check both links and together they have all the information you need to achieve what you are requesting. DO some work, show what you did if you get stuck and maybe then the community will try to help you rather than do the work for you. – Nkosi Feb 05 '16 at 14:07

1 Answers1

5

So here is what i came up with, it's exactly what i wanted. I hope it will be helpful.

    public virtual ActionResult Index()
    {
        var list = GetSubClasses<Controller>();

        // Get all controllers with their actions
        var getAllcontrollers = (from item in list
            let name = item.Name
            where !item.Name.StartsWith("T4MVC_") // I'm using T4MVC
            select new MyController()
            {
                Name = name.Replace("Controller", ""), Namespace = item.Namespace, MyActions = GetListOfAction(item)
            }).ToList();

        // Now we will get all areas that has been registered in route collection
        var getAllAreas = RouteTable.Routes.OfType<Route>()
            .Where(d => d.DataTokens != null && d.DataTokens.ContainsKey("area"))
            .Select(
                r =>
                    new MyArea
                    {
                        Name = r.DataTokens["area"].ToString(),
                        Namespace = r.DataTokens["Namespaces"] as IList<string>,
                    }).ToList()
            .Distinct().ToList();


        // Add a new area for default controllers
        getAllAreas.Insert(0, new MyArea()
        {
            Name = "Main",
            Namespace = new List<string>()
            {
                typeof (Web.Controllers.HomeController).Namespace
            }
        });


        foreach (var area in getAllAreas)
        {
            var temp = new List<MyController>();
            foreach (var item in area.Namespace)
            {
                temp.AddRange(getAllcontrollers.Where(x => x.Namespace == item).ToList());
            }
            area.MyControllers = temp;
        }

        return View(getAllAreas);
    }

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

    private IEnumerable<MyAction> GetListOfAction(Type controller)
    {
        var navItems = new List<MyAction>();

        // Get a descriptor of this controller
        ReflectedControllerDescriptor controllerDesc = new ReflectedControllerDescriptor(controller);

        // Look at each action in the controller
        foreach (ActionDescriptor action in controllerDesc.GetCanonicalActions())
        {
            bool validAction = true;
            bool isHttpPost = false;

            // Get any attributes (filters) on the action
            object[] attributes = action.GetCustomAttributes(false);

            // Look at each attribute
            foreach (object filter in attributes)
            {
                // Can we navigate to the action?
                if (filter is ChildActionOnlyAttribute)
                {
                    validAction = false;
                    break;
                }
                if (filter is HttpPostAttribute)
                {
                    isHttpPost = true;
                }

            }

            // Add the action to the list if it's "valid"
            if (validAction)
                navItems.Add(new MyAction()
                {
                    Name = action.ActionName,
                    IsHttpPost = isHttpPost
                });
        }
        return navItems;
    }

    public class MyAction
    {
        public string Name { get; set; }

        public bool IsHttpPost { get; set; }
    }

    public class MyController
    {
        public string Name { get; set; }

        public string Namespace { get; set; }

        public IEnumerable<MyAction> MyActions { get; set; }
    }

    public class MyArea
    {
        public string Name { get; set; }

        public IEnumerable<string> Namespace { get; set; }

        public IEnumerable<MyController> MyControllers { get; set; }
    }

For getting actions, i used this answer.

If you have any better ways, please let me know.

Community
  • 1
  • 1
kamal hamidi
  • 665
  • 1
  • 6
  • 16