[英]How to stop execution process in MVC 5 with C#?
What line of code do I need in the TODO
section? 我需要在TODO
部分中使用哪一行代码?
My application code is: 我的应用程序代码是:
/// <summary>
/// Override onAuthorization filter is use for authorization use request.
/// </summary>
/// <param name="filterContext"></param>
protected override void OnAuthorization(AuthorizationContext filterContext)
{
AuthorizationManager authorizationManager = new AuthorizationManager();
string FilePath = Convert.ToString(filterContext.HttpContext.Request.FilePath);
if (!authorizationManager.IsAuthorized(_userSession, FilePath))
{
RedirectToControllers(ControllerHelper.Controller.ACCOUNT, ControllerHelper.Controller.Action.ACCOUNT_LOGIN);
//TODO:
// 1. Need to stop execution process from here.
// 2. No need to execute any line of code from here.
// I have tested return/Break not working here.
}
}
Change the RedirectToControllers
to return an ActionResult then set the filterContext.Result . 更改RedirectToControllers
以返回ActionResult,然后设置filterContext.Result。
//filterContext.Result = new RedirectResult("~/account/login");
filterContext.Result = RedirectToControllers(ControllerHelper.Controller.ACCOUNT, ControllerHelper.Controller.Action.ACCOUNT_LOGIN);
Have you tried using break
to get out of the if
? 您是否尝试过使用break
来摆脱if
?
protected override void OnAuthorization(AuthorizationContext filterContext)
{
AuthorizationManager authorizationManager = new AuthorizationManager();
string FilePath = Convert.ToString(filterContext.HttpContext.Request.FilePath);
if (!authorizationManager.IsAuthorized(_userSession, FilePath))
{
RedirectToControllers(ControllerHelper.Controller.ACCOUNT, ControllerHelper.Controller.Action.ACCOUNT_LOGIN);
break; // use this
return; // or this
// 1. Need to stop excution process from here.
// 2. No need to excute any line of code from here.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.