/// <summary>
/// 获取所有的action
/// </summary>
/// <returns></returns>
public HttpResponseMessage GetActionList()
{
List<ActionModel> actionModels = new List<ActionModel>();
// 获取区域名称,限制Areas后面跟区域,不允许再深入
string areaName = string.Empty;
object[] attrs;
// 获取当前运行的程序集的反射对象
var assembly = Assembly.GetExecutingAssembly();
// 所有类型的集合
foreach (var type in assembly.GetTypes())
{
// 不属于controller的跳过
if (!typeof(ApiController).IsAssignableFrom(type)) continue;
// 处理Areas,无法直接获取到区域名称,只能根据命名空间处理
string[] namespaceSplit = type.Namespace.Split('.');
for (int i = 0; i < namespaceSplit.Length; i++)
{
if (namespaceSplit[i] == "Areas") areaName = namespaceSplit[i + 1];
}
foreach (var member in type.GetMembers())
{
// 不是方法时,跳过
if (member.MemberType != MemberTypes.Method) continue;
//去除不需要的方法
if (member.DeclaringType.Name.Contains("ApiController") || member.DeclaringType.Name.Contains("Object")) continue;
// member.ReturnType,调试时可以看到,但实际是没有ReturnType属性的,所以采用以下判断
// 方法返回值为Void的跳过
if (member.ToString().IndexOf("Void") == 0) continue;
//获取方法说明
attrs = member.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
actionModels.Add(new ActionModel
{
AreaName = areaName,
ControllerName = type.Name.Replace("Controller", ""),
ActionName = member.Name,
Describe = attrs.Length > 0 ? (attrs[0] as System.ComponentModel.DescriptionAttribute).Description : ""
});
}
}
return JsonManager.GetSuccess(actionModels);
}
/// <summary>
/// action model
/// </summary>
public class ActionModel
{
public string AreaName { get; set; }
public string ControllerName { get; set; }
public string ActionName { get; set; }
public string Describe { get; set; }
}
参考:https://blog.csdn.net/weixin_34038652/article/details/85579455?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param