扫描站点中的所有行动

编程入门 行业动态 更新时间:2024-10-21 05:39:36
本文介绍了扫描站点中的所有行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我如何创建站点中的所有操作动作链接?结果我想将这些操作链接到一个菜单系统。

我希望我可以这样做

在控制器{的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.

更多推荐

扫描站点中的所有行动

本文发布于:2023-11-28 13:09:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:站点

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!