ASP.NET MVC:获取所有控制器 [英] ASP.NET MVC: Get all controllers
问题描述
是否有可能取得所有控制器到的ControllerFactory?结果
我想要做的就是在应用程序中所有控制器类型的列表,但以一致的方式。
Is it possible to get all controllers available to a ControllerFactory?
What I want to do is get a list of all controller types in application, but in a consistent way.
让所有的控制器我得到的是同样的那些默认的请求分辨率使用。
So that all controllers I get are the same ones default request resolution is using.
(实际的任务是找到所有的操作方法有一个给定的属性)。
(The actual task is to find all action methods that have a given attribute).
推荐答案
您可以使用反射来枚举所有类的组件,并仅过滤类继承自Controller类。
You can use reflection to enumerate all classes in an assembly, and filter only classes inherit from Controller class.
最好的参考就是 asp.net的MVC源$ C $ C 。看一看的<一的实现href=\"http://aspnet.$c$cplex.com/sourcecontrol/changeset/view/23011?projectName=aspnet#266456\">ControllerTypeCache和<一个href=\"http://aspnet.$c$cplex.com/sourcecontrol/changeset/view/23011?projectName=aspnet#266437\">ActionMethodSelector类。
ControllerTypeCache演示了如何获取所有控制器类可用。
The best reference is asp.net mvc source code. Take a look of the implementations of ControllerTypeCache and ActionMethodSelector class. ControllerTypeCache shows how to get all controller classes available.
internal static bool IsControllerType(Type t) {
return
t != null &&
t.IsPublic &&
t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) &&
!t.IsAbstract &&
typeof(IController).IsAssignableFrom(t);
}
public void EnsureInitialized(IBuildManager buildManager) {
if (_cache == null) {
lock (_lockObj) {
if (_cache == null) {
List<Type> controllerTypes = GetAllControllerTypes(buildManager);
var groupedByName = controllerTypes.GroupBy(
t => t.Name.Substring(0, t.Name.Length - "Controller".Length),
StringComparer.OrdinalIgnoreCase);
_cache = groupedByName.ToDictionary(
g => g.Key,
g => g.ToLookup(t => t.Namespace ?? String.Empty, StringComparer.OrdinalIgnoreCase),
StringComparer.OrdinalIgnoreCase);
}
}
}
}
和ActionMethodSelector说明如何检查如果一个方法所需的属性。
And ActionMethodSelector shows how to check if a method has desired attribute.
private static List<MethodInfo> RunSelectionFilters(ControllerContext controllerContext, List<MethodInfo> methodInfos) {
// remove all methods which are opting out of this request
// to opt out, at least one attribute defined on the method must return false
List<MethodInfo> matchesWithSelectionAttributes = new List<MethodInfo>();
List<MethodInfo> matchesWithoutSelectionAttributes = new List<MethodInfo>();
foreach (MethodInfo methodInfo in methodInfos) {
ActionMethodSelectorAttribute[] attrs = (ActionMethodSelectorAttribute[])methodInfo.GetCustomAttributes(typeof(ActionMethodSelectorAttribute), true /* inherit */);
if (attrs.Length == 0) {
matchesWithoutSelectionAttributes.Add(methodInfo);
}
else if (attrs.All(attr => attr.IsValidForRequest(controllerContext, methodInfo))) {
matchesWithSelectionAttributes.Add(methodInfo);
}
}
// if a matching action method had a selection attribute, consider it more specific than a matching action method
// without a selection attribute
return (matchesWithSelectionAttributes.Count > 0) ? matchesWithSelectionAttributes : matchesWithoutSelectionAttributes;
}
这篇关于ASP.NET MVC:获取所有控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!