【问题标题】:ASP.Net MVC Redirect out of a partial view from controller to a full view from a different controllerASP.Net MVC 从控制器的部分视图重定向到不同控制器的完整视图
【发布时间】:2014-10-29 18:50:18
【问题描述】:

好的。所以我有一个问题,我需要在控制器操作中进行一些授权检查。

有授权角色,但可以存在有人有TypeOnePayment,但没有TypeTwo

[Authorize(Roles = "TypeOnePayment;TypeTwoPayment")]
public ActionResult EnterRevenue(PaymentType payment)
{
    payment = "TypeOne"; // This exists for show only.
    var permission = string.Concat(payment,"Permission");
    if (!SecurityUtility.HasPermission(permission))
    {
        return View("Unauthorized", "Error");
    }
    return this.PartialView("_EnterRevenue");
}

但由于这是返回局部视图,因此“错误”屏幕仅出现在页面的局部视图部分。有没有办法重定向到一个全新的页面?

编辑:正在通过 ajax 调用检索 EnterRevenue。因此,只有 html 被返回,并且被放置在调用它的视图中。

【问题讨论】:

  • 你在哪里使用这个局部视图?在 ajax 或 Html.Action 中?
  • 使用ajax调用检索,调用成功后新的html被插入到html中。
  • 你应该阅读这个:stackoverflow.com/questions/199099/…

标签: asp.net-mvc razor


【解决方案1】:

您可以重定向到其他操作:

public ActionResult EnterRevenue
{
    if (!SecurityUtility.HasPermission(permission))
    {
        return View("Unauthorized", "Error");
    }
    return RedirectToAction("NotAuthorized","Error");
}

假设我们有ErrorControllerNotAuthorized 操作,它返回正常视图,显示您无权查看此页面。

如果您需要对每个操作进行此检查,那么您需要实现自定义操作过滤器属性,您必须在其中检查它是否是正常的请求重定向,否则返回状态为 json 并从客户端重定向。见asp.net mvc check if user is authorized before accessing page

这是一段代码:

public class AuthorizationAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string actionName = filterContext.ActionDescriptor.ActionName;
            string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;


            if (filterContext != null)
            {
                HttpSessionStateBase objHttpSessionStateBase = filterContext.HttpContext.Session;
                var userSession = objHttpSessionStateBase["userId"];
                if (((userSession == null) && (!objHttpSessionStateBase.IsNewSession)) || (objHttpSessionStateBase.IsNewSession))
                {
                    objHttpSessionStateBase.RemoveAll();
                    objHttpSessionStateBase.Clear();
                    objHttpSessionStateBase.Abandon();
                    if (filterContext.HttpContext.Request.IsAjaxRequest())
                    {
                        filterContext.HttpContext.Response.StatusCode = 403;
                        filterContext.Result = new JsonResult { Data = "LogOut" };
                    }
                    else
                    {
                        filterContext.Result = new RedirectResult("~/Home/Index");
                    }

                }


                else
                {

                    if (!CheckAccessRight(actionName, controllerName))
                    {
                        string redirectUrl = string.Format("?returnUrl={0}", filterContext.HttpContext.Request.Url.PathAndQuery);

                        filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl + redirectUrl, true);
                    }
                    else
                    {
                        base.OnActionExecuting(filterContext);
                    }
                }


            }

        }
 }

并将其用于如下操作:

[Authorization]
public ActionResult EnterRevenue
{
    return this.PartialView("_EnterRevenue");
}

【讨论】:

  • 我可能措辞不佳。但 EnterRevenue 是部分观点。因此,无论返回什么,它都只会显示在调用它的主视图的部分视图空间中。
  • 您如何调用 EventRevenue 操作?显示那段代码?
  • 编辑了我的帖子,但正在通过 ajax 调用检索 EnterRevenue。因此,只是返回了 html,并将其放置在调用它的视图中。
  • 所以可能有一些写得不好的要求,但我们在这些页面上设置了授权。但本质上,我们有许多不同的支付类型,因此需要为每一种支付单独的支票。我会相应地更新代码。
  • 是的,您应该清楚地告诉并添加相关代码,以免您的帖子被误解
【解决方案2】:

或者只使用标准的重定向调用。这应该适用于任何地方(只是不要在 using 语句中执行此操作,否则它会在后台抛出异常):

Response.Redirect("/Account/Login?reason=NotAuthorised", true);                         

【讨论】:

    【解决方案3】:

    我认为您需要的内容可以归结为一种方式,让 ajax 调用根据您返回的内容而有所不同。我发现这样做的最好方法可以总结如下:

    • 当您检测到您没有权限时,将模型状态错误添加到您的模型中。
    • 覆盖 OnActionExecuted(希望您的所有控制器都从基础控制器继承,这样您就可以在一个地方执行此操作,如果不是,现在实现它可能是个好主意)。在 override 中,检查请求是否为 ajax 且模型状态无效(如果您愿意,可以检查您在 action 方法中添加的具体错误),将请求状态更改为 4xx 状态。
    • 在 ajax 调用的 OnFailure 中,您可以使用 javascript 代码重定向到错误页面。

    【讨论】:

      猜你喜欢
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2017-09-25
      • 2018-05-25
      • 1970-01-01
      • 2021-09-05
      相关资源
      最近更新 更多