1.获取所有的Controllers,通过反射获取
//获取某一个程序集下所有的类
private static List<Type> GetSubClasses<T>()
{
return Assembly.GetCallingAssembly().GetTypes().Where(
type => type.IsSubclassOf(typeof(T))).ToList();
}
2.获取Controller下的action
private static List<MethodInfo> GetSubMethods(Type t)
{
return t.GetMethods().Where(m => m.ReturnType == typeof(ActionResult) && m.IsPublic == true).ToList();
}
3.遍历得到所有Contrller和Actions
public List<string> GetControllerNames()
{
List<string> controllerNames = new List<string>();
foreach (var t in GetSubClasses<Controller>())
{
controllerNames.Add(t.Name);
List<MethodInfo> mfCollection = GetSubMethods(t);
mfCollection.ForEach(method => controllerNames.Add("---" + method.Name));
}
return controllerNames;
}