c# - Get all Area, Controller and Action as a tree -


i need areas has been registered list , of controllers sub list , same thing actions. this:

adminarea    homecontroller      index      add      ...    othercontroller    ... anotherarea    homecontroller       index       ... ... 

i have checked answer of this question, , one, not i'm looking for. first 1 return routes has been registered , second 1 return controllers @ once.

update

ok, below code can areas , answer of second question can controllers, can't figure out each controller belong area.

var areanames = routetable.routes.oftype<route>()             .where(d => d.datatokens != null && d.datatokens.containskey("area"))             .select(r => r.datatokens["area"]).tolist()             .distinct().tolist(); 

so here came with, it's wanted. hope helpful.

    public virtual actionresult index()     {         var list = getsubclasses<controller>();          // controllers actions         var getallcontrollers = (from item in list             let name = item.name             !item.name.startswith("t4mvc_") // i'm using t4mvc             select new mycontroller()             {                 name = name.replace("controller", ""), namespace = item.namespace, myactions = getlistofaction(item)             }).tolist();          // areas 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"] ilist<string>,                     }).tolist()             .distinct().tolist();           // add new area 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>();          // descriptor of controller         reflectedcontrollerdescriptor controllerdesc = new reflectedcontrollerdescriptor(controller);          // @ each action in controller         foreach (actiondescriptor action in controllerdesc.getcanonicalactions())         {             bool validaction = true;             bool ishttppost = false;              // attributes (filters) on action             object[] attributes = action.getcustomattributes(false);              // @ each attribute             foreach (object filter in attributes)             {                 // can navigate action?                 if (filter childactiononlyattribute)                 {                     validaction = false;                     break;                 }                 if (filter httppostattribute)                 {                     ishttppost = true;                 }              }              // add action 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, used answer.

if have better ways, please let me know.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -