【问题标题】:Ignore @Html.Action() if user not in Role如果用户不在角色中,则忽略 @Html.Action()
【发布时间】:2013-06-26 06:31:31
【问题描述】:

在我的视图中,我渲染菜单@Html.Action("RenderMenu", "Admin")@Html.Action("RenderMenu", "Manager") 哪里行动:

    [Authorize(Roles = "Admin")]
    public ActionResult RenderMenu()
    {
        return View();
    }

如果用户不是角色管理员

@Html.Action("RenderMenu", "Admin") 忽略。

我该怎么做? 现在,我有无限的登录窗口。

【问题讨论】:

    标签: asp.net-mvc attributes


    【解决方案1】:

    看我的回答 Asp.net MVC3: is really based on roles @Html.Action()

    你可以使用:

    @Html.ActionBaseRole("RenderMenu", "Admin")
    

    【讨论】:

      【解决方案2】:

      在 razor 视图中创建 if 以检查用户是否处于所需角色。您可以使用 (User.IsInRole("Adm inistrators"))

      这里有类似的讨论asp.net MVC3 razor: display actionlink based on user role

      【讨论】:

        【解决方案3】:

        你可以用这个:

        @if (Page.User.IsInRole("Admin")) {   
            @Html.Action("RenderMenu", "Admin")
        }
        

        【讨论】:

          【解决方案4】:

          我有一个答案。你可以试试......

          @{
              if(User.IsInRole("Admin"))
                 {
                    @Html.Action("RenderMenu", "Admin")
                    @Html.Action("RenderMenu", "Manager")
                 }
             else
                {
          @Html.Action("RenderMenu", "Manager")
          
               }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-31
            • 1970-01-01
            • 2017-02-15
            • 2021-10-15
            • 1970-01-01
            相关资源
            最近更新 更多