asp.net-mvc – ASP.NET MVC:获取所有控制器
发布时间:2020-05-24 01:23:30 所属栏目: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 – 如何根据用户的角色创建具有不同显示的视图?
- asp.net – 通过在弹性beanstalk的负载均衡器中的IIS重写到
- asp.net-mvc – MVC ASP.NET或Razor
- 使用TypeScript装饰器会导致错误
- asp.net – viewstate到期了吗?
- asp.net-mvc-3 – 处理DbContext后的问题
- asp.net-mvc-4 – 如何为Web API控制器方法指定ContentType
- asp.net-core-mvc – Html.AntiForgeryToken()仍然需要?
- ASP.Net:如果我有Session ID,可以获取Session对象吗?
- asp.net-mvc – Razor – @ Html.Raw()仍在编码在元标记属性
推荐文章
站长推荐
- asp.net-mvc – MVC4 SimpleMemberhip’提供商遇
- CORS支持PUT和DELETE与ASP.NET Web API
- 要监控已部署的ASP.NET Web应用程序的关键性能指
- 将Funscript添加到现有的ASP.NET MVC项目中
- asp.net – 使用纯CSS定义asp:GridView的全局网
- asp.net – MSDeploy.exe可以作为管理员连接,但不
- Azure上的Asp.Net Core 2.0产生了502.5
- asp.net-core – 重新挑战ASP.NET Core中经过身份
- asp.net – 在部署过程中如何显示维护页面?
- asp.net-web-api – 无法使MVC4 WebApi包含JSON中
热点阅读
