asp.net mvc - How can I get the list of all actions of MVC Controller by passing ControllerName? -
how can list of actions of controller? search cannot find example/answer. see example recommended using reflection don't know how.
here trying do:
public list<string> actionnames(string controllername){ }
you haven't told why need 1 possibility use reflection:
public list<string> actionnames(string controllername) { var types = in appdomain.currentdomain.getassemblies() t in a.gettypes() typeof(icontroller).isassignablefrom(t) && string.equals(controllername + "controller", t.name, stringcomparison.ordinalignorecase) select t; var controllertype = types.firstordefault(); if (controllertype == null) { return enumerable.empty<string>().tolist(); } return new reflectedcontrollerdescriptor(controllertype) .getcanonicalactions().select(x => x.actionname) .tolist(); }
obviously know reflection not fast if intend call method might consider improving caching list of controllers avoid fetching everytime , memoizing method given input parameters.
Comments
Post a Comment