【发布时间】:2017-06-09 10:25:28
【问题描述】:
如何在 dotnet core 中找到所有具有其属性的控制器和动作? 在 .NET Framework 中,我使用了以下代码:
public static List<string> GetControllerNames()
{
List<string> controllerNames = new List<string>();
GetSubClasses<Controller>().ForEach(type => controllerNames.Add(type.Name.Replace("Controller", "")));
return controllerNames;
}
public static List<string> ActionNames(string controllerName)
{
var types =
from a in AppDomain.CurrentDomain.GetAssemblies()
from t in a.GetTypes()
where typeof(IController).IsAssignableFrom(t) &&
string.Equals(controllerName + "Controller", t.Name, StringComparison.OrdinalIgnoreCase)
select t;
var controllerType = types.FirstOrDefault();
if (controllerType == null)
{
return Enumerable.Empty<string>().ToList();
}
return new ReflectedControllerDescriptor(controllerType)
.GetCanonicalActions().Select(x => x.ActionName).ToList();
}
但它在 dotnet core 中不起作用。
【问题讨论】:
标签: c# asp.net-core asp.net-core-mvc