MVC获取Controller和Action
/// <summary> /// 读取项目中的Controller和Action,记录到数据库中 /// </summary> public void Init() { //控制器类型列表 List<Type> controllerTypes = new List<Type>(); //加载程序集 var assembly = Assembly.Load(Conn.CreateInstance().AssemblyName); //获取程序集下所有的类 controllerTypes.AddRange(assembly.GetTypes().Where(type => typeof(Controller).IsAssignableFrom(type) && type.Name != "ErrorController")); List<ConAct> cons = new List<ConAct>(); foreach (var item in controllerTypes) { ConAct con = new ConAct(); //控制器的全名称 string controllerName = item.FullName; con.ConName = controllerName; //获取控制器的方法 var methods = item.GetMethods().Where(method => method.ReturnType.Name == "ActionResult" || method.ReturnType.Name == "IActionResult"); con.ActNames = new List<string>(); foreach (var method in methods) { if (method.Name != "ValidationProblem") { con.ActNames.Add(method.Name); } } cons.Add(con); } SetConAct(cons); }