基于角色的导航 [英] Role Based Navigation

查看:193
本文介绍了基于角色的导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图想出一个办法来为我工作的一个项目一个动态的基于角色的导航解决方案。

I've been trying to come up with a way to create a dynamic role based navigation solution for a project that I am working on.

导航应该只显示是相对于用户的作用环节,例如:一个的管理员的将有链接可查看应用统计,管理客户帐户,等...而标准的用户会有链接来管理他们的帐户,与朋友沟通,等..

The navigation should display only links that are relative to the users role, for example: an administrator will have links to view application statistics, manage customer accounts, ect... while a standard user would have links to manage their account, communicate with friends, ect..

我现在有称为单局部视图的导航的与角色检查一些基本的条件语句和标记来显示相应的链接组合。这工作,但我知道它可能很快变得难以管理。

I currently have a single partial view called Navigation with some basic conditional statements for role checking and a mix of markup for displaying the appropriate links. This works, but, I know it could quickly become unmanageable.

导航管窥:

@if(User.IsInRole("Admin")) {
    <li><a href="#">Statistics</a></li>
    <li><a href="#">Accounts</a></li>
    <li><a href="#">Dashboard</a></li>
} 
@if(User.IsInRole("User")) {
    <li><a href="#">Account</a></li>
    <li><a href="#">Friends</a></li>
}
// code omitted

有没有办法让这个从逻辑视图,让控制器处理呢?

Is there a way to get this logic out of the view and let the Controller handle this?

推荐答案

SOLUTION

至于建议,我创建了一个名为菜单,并为每个角色局部视图ChildAction。里面的动作我做的一些使用条件语句和渲染相应的视图一些角色检查。

As suggested, I created a ChildAction called Menu and partial views for each role. Inside the action I do some role checking using some conditional statements and render the appropriate view.

这保持了条件语句出来,这使得它有很多清晰的解决方案。

This keeps the conditional statements out of the views which makes it a lot cleaner solution.

我敢肯定有可以做,整理一下几件事情,我会继续努力改善它。

I'm sure there are a few things that could be done to tidy it up and I will continue trying to improve it.

下面是我使用的解决方案。

Here is the solution I used.

在这里我想显示我用这个菜单的布局视图。

In the layout view where I wanted the menu to appear I used this.

@Html.Action("Menu", "Navigation")

然后,我创建了一个名为导航控制器,并添加一个名为行动菜单。

Then I created a controller called Navigation and added a single Action called Menu.

public class NavigationController : Controller
{

    [ChildActionOnly]
    public ActionResult Menu()
    {
        if (Roles.IsUserInRole("Administrator"))
        {
            return PartialView("_NavigationAdmin");

        }

        return PartialView("_NavigationPublic");
    }

}

这篇关于基于角色的导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆