在 MVC 中,如何使用控制器为已批准的用户呈现局部视图? [英] In MVC how can I use the controller to render a partial view only for approved users?

查看:29
本文介绍了在 MVC 中,如何使用控制器为已批准的用户呈现局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MVC 5 中,仅当(Windows 身份验证)用户属于一个或多个 Active Directory 组列表时,我才尝试使用控制器呈现部分视图.我需要考虑 30 多个不同的组,因此hello world"示例不符合我的需要.在网上玩寻宝游戏后,我设法收集了这么多.没有编译或运行时错误,但内容显示给所有用户而不是特定用户.所以还没有达到预期的结果.

虽然我可以在视图中使用 if-then 逻辑实现预期的结果,但它会产生大量不必要的重复并鼓励意大利面化.所以我试图在控制器中做到这一点.

预期结果摘要:

当用户加载视图页面时,仅当 Windows Authenticated 用户属于控制器操作中定义的一组组列表中的一个或多个组时,才应呈现部分视图.如果用户未经授权,则不包括部分视图.

控制器块:

[ChildActionOnly][授权(角色=域\组A,域\组B")]公共 ActionResult MonitorCSU(){返回 PartialView("MonitorCSU");}

查看块

@Html.Partial("MonitorCSU")

不成功的迭代:

  • 在控制器块中,我尝试(失败)使用 if-then 块,else 情况是另一个没有内容的局部视图.

    [ChildActionOnly]
    公共 ActionResult MonitorCSU(){if (User.IsInRole("DomainGroupA")) {返回 PartialView("_MonitorCSU");}别的{return PartialView("_Unauthorized");}}

  • 在 Razor 中,我尝试使用 HTML.Action,但是当我尝试运行页面时,浏览器陷入无限循环.

解决方案

@Html.Partial() 返回一个局部视图而不调用控制器方法.为了调用您的控制器方法,您需要使用

@Html.Action("MonitorCSU")

@{ Html.RenderAction("MonitorCSU") }

注意这里假设 MonitorCSU() 方法与生成主视图的方法在同一个控制器中(否则你还需要包含控制器名称的参数)

参考文档

In MVC 5 I am attempting to use the controller to render a partial view only if the (Windows Authenticated) user belongs to one or more of a list of Active Directory groups. There are over 30 distinct groups I need to account for, so the "hello world" examples don't fit my needs. After playing scavenger hunt on the web, I managed to collect this much. No compile or runtime errors, but the content is showing for all users rather than the specific users. So the desired outcome is not yet achieved.

While I can achieve the desired outcome using if-then logic in the view, it creates a lot of unnecessary duplication and encourages spaghettification. So I'm trying to do this in the controller.

Summary of Desired Outcome:

When the user loads the viewpage, the partial view should only render if the Windows Authenticated user belongs to one or more of a list of groups defined in the controller action. If the user is not authorized, then the partial view is not included.

Controller Block:

[ChildActionOnly]
    [Authorize(Roles="Domain\GroupA,Domain\GroupB")]
    public ActionResult MonitorCSU()
    {   
        return PartialView("MonitorCSU");            
    }

View Block:

<div class="rowWithCols3">
@Html.Partial("MonitorCSU")

Unsuccessful Iterations:

  • In the controller block I tried (unsuccessfully) to use an if-then block, the else case being another partial view with no content.

    [ChildActionOnly]
    public ActionResult MonitorCSU() { if (User.IsInRole("DomainGroupA")) { return PartialView("_MonitorCSU"); } else { return PartialView("_Unauthorized"); } }

  • In Razor, I tried using HTML.Action but when I tried run the page the browser hung in an infinite loop.

解决方案

@Html.Partial() returns a partial view without calling a controller method. In order to call your controller method, you need to use

@Html.Action("MonitorCSU")

or

@{ Html.RenderAction("MonitorCSU") }

Note this assumes that the MonitorCSU() method is in the same controller as the method that generates the main view (other wise you also need to include a parameter for the controller name)

Refer documentation

这篇关于在 MVC 中,如何使用控制器为已批准的用户呈现局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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