Rai*_*ika 9 c# asp.net-core-mvc asp.net-core
如何在dotnet核心中查找其属性的所有控制器和操作?在.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();
}
Run Code Online (Sandbox Code Playgroud)
但它不适用于dotnet核心.
注入IActionDescriptorCollectionProvider
需要知道这些东西的组件怎么样?它在Microsoft.AspNetCore.Mvc.Infrastructure
命名空间中.
该组件为您提供应用程序中可用的每个操作.以下是它提供的数据示例:
作为奖励,您还可以评估所有过滤器,参数等.
作为旁注,我想你可以使用反射来查找继承的类型ControllerBase
.但是你知道你可以拥有不从它继承的控制器吗?您可以编写覆盖这些规则的约定吗?因此,注入上述组件使其更容易.你不必担心它会破裂.
归档时间: |
|
查看次数: |
1619 次 |
最近记录: |