Anil Singh Anil Singh - 3 months ago 20
ASP.NET (C#) Question

How to stop execution process in MVC 5 with C#?

Anyone can help me, what line of code help.

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 excution process from here.
// 2. No need to excute any line of code from here.
// I have tested return/Break not working here.
}
}

Answer

Change the RedirectToControllers to return an ActionResult then set the filterContext.Result .

//filterContext.Result = new RedirectResult("~/account/login");

filterContext.Result =  RedirectToControllers(ControllerHelper.Controller.ACCOUNT,  ControllerHelper.Controller.Action.ACCOUNT_LOGIN);
Comments