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.
1 Publicactionresult getcontrollerandaction ()2List<type> controllertypes =NewList<type> ();//Create a list of controller types3 varAssembly = Assembly.Load ("Mysoft.ui");//Loading Assemblies4Controllertypes.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 filter5StringBuilder Jsonbuilder =NewStringBuilder ();//creating dynamic strings, stitching JSON data note: JSON types now pass data in a fashion that is more concise than XML6Jsonbuilder.append ("[");7 foreach(varControllerinchControllertypes)//traversing the controller class8 {9Jsonbuilder.append ("{\ "controllername\": \ "");Ten Jsonbuilder.append (Controller. Name); OneJsonbuilder.append ("\ ", \" controllerdesc\ ": \""); AJsonbuilder.append (Controller. GetCustomAttribute (typeof(DescriptionAttribute)) asDescriptionAttribute) = =NULL?"": (Controller. GetCustomAttribute (typeof(DescriptionAttribute)) asDescriptionAttribute). Description);//gets the description of the controller description -Jsonbuilder.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 the 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 action +Jsonbuilder.append ("\"},"); A } atJsonbuilder.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)