Asp.net mvc ASP.NETMVC从控制器的局部视图重定向到另一个控制器的完整视图
嗯。因此,我有一个问题,我需要在控制器操作中执行一些授权检查 有授权角色,但也可以有人拥有TypeOnePayment,而不是TypeTwoAsp.net mvc ASP.NETMVC从控制器的局部视图重定向到另一个控制器的完整视图,asp.net-mvc,razor,Asp.net Mvc,Razor,嗯。因此,我有一个问题,我需要在控制器操作中执行一些授权检查 有授权角色,但也可以有人拥有TypeOnePayment,而不是TypeTwo [Authorize(Roles = "TypeOnePayment;TypeTwoPayment")] public ActionResult EnterRevenue(PaymentType payment) { payment = "TypeOne"; // This exists for show only. var permiss
[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,并将其放在调用它的视图中。您可以重定向到其他操作:
public ActionResult EnterRevenue
{
if (!SecurityUtility.HasPermission(permission))
{
return View("Unauthorized", "Error");
}
return RedirectToAction("NotAuthorized","Error");
}
假设我们有ErrorController
,操作NotAuthorized
,返回正常视图,显示您无权查看此页面
如果您需要对每个操作进行此检查,那么您需要实现自定义操作过滤器属性,在该属性中,您必须检查它是否是正常的请求重定向,否则将staus作为json返回,并从客户端重定向。看
下面是一段代码:
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");
}
我认为,您需要的可以归结为ajax调用根据您返回的内容表现出不同的方式。我在这方面找到的最好的方法可以总结如下:
- 当检测到没有权限时,请向模型中添加模型状态错误
- 重写OnActionExecuted(希望您的所有控制器都继承自一个基本控制器,这样您就可以在一个地方执行它,如果不是的话,现在实现它可能是一个好主意)。在覆盖中,检查请求是否为ajax且模型状态无效(如果需要,可以检查在操作方法中添加的特定错误),将请求状态更改为4xx状态
- 在ajax调用失败时,可以使用javascript代码重定向到错误页面
语句内部使用语句,否则它会在后台抛出异常):
您在哪里使用此局部视图?在ajax或Html.Action中?它是使用ajax调用检索的,调用成功后新的Html将插入Html中。您应该阅读以下内容:我可能用词不好。但收入是部分观点。因此,无论返回什么,它都只显示在调用它的主视图的部分视图空间中。如何调用EventRevenue操作?显示那段代码?编辑了我的文章,但EnterRevenue是通过ajax调用检索的。因此,只返回html,并将其放在调用它的视图中。因此,可能存在一些写得不好的需求,但我们在这些页面上设置了授权。但本质上我们有许多不同的支付类型,所以每种类型都需要单独的支票。我会相应地更新代码。是的,你应该清楚地说出并添加相关代码,这样你的帖子就不会被误解
Response.Redirect("/Account/Login?reason=NotAuthorised", true);