ASP.Net MVC将部分视图从控制器重定向到不同控制器的完整视图

时间:2014-10-29 18:50:18

标签: asp.net-mvc razor

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

有授权角色,但可能存在某人有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,它被放置在从它调用的视图中。

3 个答案:

答案 0 :(得分:5)

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

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

假设我们ErrorController行动NotAuthorized,会返回正常视图,显示您无权查看此页面。

如果您需要检查每个操作,那么您需要实现自定义操作过滤器属性,您必须在其中检查是否是正常请求重定向,否则将staus返回为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");
}

答案 1 :(得分:0)

我认为你需要的东西可以归结为ajax调用根据你返回它的方式表现不同的方式。我发现这样做的最好结论可归纳如下:

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

答案 2 :(得分:0)

或者只是使用标准的重定向呼叫。这应该适用于所有地方(只是不在using语句中执行它或它将在后台抛出异常):

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