In the development of MVC project encountered problems, do permissions control, through the MVC filter to achieve, so in the allocation of permissions when you want to get all the Controller and action method, by looking for data, referring to the "ASP. NET MVC Framework Disclosure", the final realization.
In C #, the implementation of dynamic fetch classes and methods is mainly implemented by reflection, to refer to System.Reflection.
Publicactionresult getcontrollerandaction () List<Type> controllertypes =NewList<type> ();//Create a list of controller types varAssembly = Assembly.Load ("Mysoft.ui");//Loading AssembliesControllertypes.addrange (assembly. GetTypes (). Where (type =typeof(IController). IsAssignableFrom (type) && type. name!="Errorcontroller"));//gets all the classes under the Assembly, inheriting all the types of the IController class through a LINQ filterStringBuilder Jsonbuilder =NewStringBuilder ();//creating dynamic strings, stitching JSON data note: JSON types now pass data in a fashion that is more concise than XMLJsonbuilder.append ("["); foreach(varControllerinchControllertypes)//traversing the controller class{jsonbuilder.append ("{\ "controllername\": \ ""); Jsonbuilder.append (Controller. Name); Jsonbuilder.append ("\ ", \" controllerdesc\ ": \""); Jsonbuilder.append (Controller. GetCustomAttribute (typeof(DescriptionAttribute)) asDescriptionAttribute) = =NULL?"": (Controller. GetCustomAttribute (typeof(DescriptionAttribute)) asDescriptionAttribute). Description);//gets the description of the controller descriptionJsonbuilder.append ("\ ", \" action\ ": ["); varactions = Controller. GetMethods (). Where (method = method). Returntype.name = ="ActionResult");//get all the return type ActionResult under the controller, the permissions control of MVC as long as the limit so the front and back of the interaction request line, unified for ActionResult foreach(varActioninchactions) {Jsonbuilder.append ("{\ "actionname\": \ ""); Jsonbuilder.append (action. Name); Jsonbuilder.append ("\ ", \" actiondesc\ ": \""); Jsonbuilder.append (action. GetCustomAttribute (typeof(DescriptionAttribute)) asDescriptionAttribute) = =NULL?"": (Action. GetCustomAttribute (typeof(DescriptionAttribute)) asDescriptionAttribute). Description);//get a description of the actionJsonbuilder.append ("\"},"); } jsonbuilder.remove (Jsonbuilder.length-1,1); Jsonbuilder.append ("]},"); } jsonbuilder.remove (Jsonbuilder.length-1,1); Jsonbuilder.append ("]"); returnContent (Jsonbuilder.tostring (),"Json/text"); t");
C # Get classes and methods under classes (for ASP. NET MVC)