asp.net-mvc – ASP.NET MVC:获取所有控制器
发布时间:2020-12-15 23:37:49 所属栏目:asp.Net 来源:网络整理
导读:是否可以让ControllerFactory的所有控制器可用? 我想要做的是在应用程序中获取所有控制器类型的列表,但是以一致的方式. 所以我得到的所有控制器是默认请求分辨率正在使用的相同的. (实际的任务是找到具有给定属性的所有动作方法). 解决方法 您可以使用反射
是否可以让ControllerFactory的所有控制器可用?
我想要做的是在应用程序中获取所有控制器类型的列表,但是以一致的方式. 所以我得到的所有控制器是默认请求分辨率正在使用的相同的. (实际的任务是找到具有给定属性的所有动作方法). 解决方法
您可以使用反射枚举程序集中的所有类,并仅过滤继承自Controller类的类.
最好的参考是asp.net mvc source code.看看ControllerTypeCache和ActionMethodSelector类的实现. 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显示如何检查方法是否具有所需属性. 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 – URL重写规则在Visual Studio Development Serve
- asp.netcore 自动挡Docker Nginx Redis(滴滴滴,自动挡)
- asp.net-mvc – ModelState.IsValid总是返回false
- asp.net – 如何显示如果绑定数据源是List Collection,则在
- 如何在ASP.NET MVC 4 Beta中禁用Javascript/CSS缩小
- SignalR(.NET Core)中的JWT身份验证,而不在查询字符串中传递
- asp.net-mvc-5 – MVC 5 – 向用户添加声明
- asp.net 虹软人脸识别 释放内存
- asp.net – 将MVC 5降级为mvc 4
- asp.net-mvc – Asp.net Identity使用什么算法来加密密码?
推荐文章
站长推荐
- asp.net core 2.0 webapi集成signalr(实例讲解)
- asp.net – 使用StartMode =“AlwaysRunning”在
- asp.net – Shutterfly Order API.
- asp.net-mvc – 设置下拉项目选择MVC
- asp.net-mvc – 获取ActionName,ControllerName和
- asp.net-mvc-3 – 验证摘要消息中的链接
- asp.net – 具有2个不同域的mvc3路由
- asp.net – 根据IP地址限制对Elmah的访问
- asp.net-mvc中的Reportviewer数据源
- .net – 如何在同一个域但不同的主机上共享本地存
热点阅读