扫描网站中的所有操作
如何为网站中的所有操作创建操作链接?
我想将这些操作链接放到菜单系统中。扫描网站中的所有操作
我希望我可以这样做
foreach controller in controllers {
foreach action in controller{
stringbuilder.writeline(
"<li>"+actionlink(menu, action, controller)+"<li>"
);
}
}
这是我对此采取:
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();
的方法GetActions
如下:
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;
}
如果您需要的菜单系统结帐MVC Sitemap Provider,它会给你什么来呈现绝对的控制权取决于您在成员资格实施中定义的角色。
这里是方法如何从控制器Asp.net Mvc: List all the actions on a controller with specific attribute或Accessing the list of Controllers/Actions in an ASP.NET MVC application 所有动作对于实现你的目标,你会发现在你的项目中的所有控制器,Assembly.GetExportedTypes()
和过滤器只子来自第二链路的ControllerBase
和每个控制器呼叫new ReflectedControllerDescriptor(typeof(TController)).GetCanonicalActions()
的类别。
谢谢,我会在星期一检查这些。 – 2013-04-06 22:39:10
这些帖子只能在当前控制器上使用。艾哈迈德的这篇文章更全面,所以我会将其标记为答案。 – 2013-04-08 16:39:59
我看了看提供者,也许我需要再看一点,但它看起来像我需要通过xml定义菜单? – 2013-04-06 22:38:15
是的,你将不得不使用XML定义菜单。但是,您可以使用我提供的代码获取初始列表(您仍然需要定义哪些角色可以访问哪个操作) – amhed 2013-04-07 02:07:32
我们有一个基于自定义权限的授权,允许用户定义该权限。它是一个多租户系统,因此它要么必须基于表或基于约定。我们试图让后者工作。 – 2013-04-08 16:01:58