本文介绍了扫描站点中的所有行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何创建站点中的所有操作动作链接?结果我想将这些操作链接到一个菜单系统。
我希望我可以这样做
在控制器{的foreach控制器 在控制器的foreach行动{ stringbuilder.writeline( <立GT;+ ActionLink的(菜单,动作,控制器)+<立GT; ); }}解决方案
下面是我对此采取:
VAR控制器= Assembly.GetCallingAssembly()GetTypes()式(类型=> type.IsSubclassOf(typeof运算(控制器)))。了ToList();VAR controlList = controllers.Select(控制器=> 新 { 操作=的getActions(控制器), 名称= controller.Name, })了ToList()。该方法的getActions 如下:
公共静态列表<串GT;的getActions(控制器类型){ //链接列表 VAR项目=新的List<串GT;(); //获取该控制器的描述符 VAR controllerDesc =新ReflectedControllerDescriptor(控制器); //看看控制器每个动作 的foreach(在controllerDesc.GetCanonicalActions变种动作()) { //获取有关该诉讼的任何属性(过滤器) VAR属性= action.GetCustomAttributes(假); //看看每个属性 VAR validAction = attributes.All(过滤= GT;!(过滤器是HttpPostAttribute)及和放大器;!(过滤器是ChildActionOnlyAttribute)); //动作添加到列表中,如果是有效 如果(validAction) items.Add(action.ActionName); } 返回的物品;}如果你需要一个菜单系统结帐 MVC网站地图提供商,它会给你绝对的控制权是什么这取决于你对你的会员实现定义的角色来呈现。
How do I create action links for all actions in the site? I want to put those action links into a menu system.
I was hoping I can do something like
foreach controller in controllers { foreach action in controller{ stringbuilder.writeline( "<li>"+actionlink(menu, action, controller)+"<li>" ); } }解决方案
Here's my take on it:
var controllers = Assembly.GetCallingAssembly().GetTypes().Where(type => type.IsSubclassOf(typeof(Controller))).ToList(); var controlList = controllers.Select(controller => new { Actions = GetActions(controller), Name = controller.Name, }).ToList();The method GetActions as follows:
public static List<String> GetActions(Type controller) { // List of links var items = new List<String>(); // Get a descriptor of this controller var controllerDesc = new ReflectedControllerDescriptor(controller); // Look at each action in the controller foreach (var action in controllerDesc.GetCanonicalActions()) { // Get any attributes (filters) on the action var attributes = action.GetCustomAttributes(false); // Look at each attribute var validAction = attributes.All(filter => !(filter is HttpPostAttribute) && !(filter is ChildActionOnlyAttribute)); // Add the action to the list if it's "valid" if (validAction) items.Add(action.ActionName); } return items; }If you need a menu system checkout the MVC Sitemap Provider, it will give you absolute control on what to render depending on the roles you've defined on your membership implementation.
更多推荐
扫描站点中的所有行动
发布评论