构建ASP.NET MVC母版页菜单动态,根据目前用户"作用" [英] Building an ASP.NET MVC Master Page Menu Dynamically, Based on the current User's "Role"

查看:313
本文介绍了构建ASP.NET MVC母版页菜单动态,根据目前用户"作用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些类似的问题,但没有一个看起来像我想要做的事情。

这是我目前的执行情况瓦特/出任何安全:

< D​​IV ID =MenuContainer中>     < UL ID =菜单>         <李><%= Html.ActionLink(主目录,索引,AController)%>< /李>         <李><%= Html.ActionLink(产品目录,索引,BController)%>< /李>         <李><%= Html.ActionLink(企业名录,索引,CController)%>< /李>         <李><%= Html.ActionLink(用户列表,索引,DController)%>< /李>     < / UL> < / DIV>

这是好的,和上述作品。我有[授权]属性设置到prevent未经授权的访问操作的CController和DController - 但我想从菜单中删除这些项目为用户谁不正确的角色,因为当他们看到它并点击它,它会告诉他们,他们没有权限,他们会需要它。如果他们不知道它的存在,这还只是涉及到每一个人......

更好

这样的事情是最终的目标,我想那啥,但我正在寻找更多的MVC风味的形式给出,这里的观点是哑巴:

< D​​IV ID =MenuContainer中>     < UL ID =菜单>         <李><%= Html.ActionLink(主目录,索引,AController)%>< /李>         <李><%= Html.ActionLink(产品目录,索引,BController)%>< /李>         <%如果(角色= Roles.Admin){%GT;         <李><%= Html.ActionLink(企业名录,索引,CController)%>< /李>         <李><%= Html.ActionLink(用户列表,索引,DController)%>< /李>         <%}%GT;     < / UL> < / DIV>

解决方案

我做了这样的事情:

  • 使用我的控制器的通用基类(层超')
  • 在BaseController,覆盖OnActionExecuted(你也可以定义此一个ActionFilter属性)

事情是这样的:

 保护覆盖无效OnActionExecuted(ActionExecutedContext filterContext)
    {
        //构建菜单项列表根据用户的权限,并将其添加到的ViewData
        IEnumerable的<菜单项>菜单= BuildMenu();
        计算机[菜单] =菜单;
    }
 

在母版页:

 <%VAR模型=计算机[菜单]为IEnumerable<菜单项取代; %>
    <%Html.RenderPartial(菜单,模型); %>
 

(注:在现实中,我有一个包含其中包括一个MasterViewModel菜单模式)

I've seen some similar questions, but none that look like what I'm trying to do.

This is my current implementation w/out any security:

<div id="menucontainer">
    <ul id="menu">              
        <li><%= Html.ActionLink("Main List", "Index", "AController")%></li>
        <li><%= Html.ActionLink("Product List", "Index", "BController")%></li>
        <li><%= Html.ActionLink("Company List", "Index", "CController")%></li>
        <li><%= Html.ActionLink("User List", "Index", "DController")%></li>
    </ul>
</div>

This is fine, and the above works. I have [Authorize] Attributes setup on the Actions for CController and DController to prevent unauthorized access -- but I'd like to remove those items from the menu for users who don't have the correct Role, because when they see it and click on it and it tells them they don't have permission, they'll want it. If they don't know it's there, that's just better for everyone involved...

Something like this is ultimately the goal I'm trying to get at, but I'm looking for the more MVC Flavored aproach, where the "view" is "dumb":

<div id="menucontainer">
    <ul id="menu">              
        <li><%= Html.ActionLink("Main List", "Index", "AController")%></li>
        <li><%= Html.ActionLink("Product List", "Index", "BController")%></li>
        <% If(Role = Roles.Admin) { %>
        <li><%= Html.ActionLink("Company List", "Index", "CController")%></li>
        <li><%= Html.ActionLink("User List", "Index", "DController")%></li>
        <% } %>
    </ul>
</div>

解决方案

I have done something like this:

  • use a common base class for my controllers ('layer supertype')
  • in the BaseController, override OnActionExecuted (you could also define an ActionFilter attribute for this)

Something like this:

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        // build list of menu items based on user's permissions, and add it to ViewData
        IEnumerable<MenuItem> menu = BuildMenu(); 
        ViewData["Menu"] = menu;
    }

In the master page:

    <% var model = ViewData["Menu"] as IEnumerable<MenuItem>; %>
    <% Html.RenderPartial("Menu", model); %>

(Note: in reality, I have a MasterViewModel that contains among others the menu model)

这篇关于构建ASP.NET MVC母版页菜单动态,根据目前用户&QUOT;作用&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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