如何使用 C# 在 MVC 5 中停止执行进程

本文关键字:执行 进程 何使用 MVC | 更新日期: 2024-10-30 09:35:38

TODO部分我需要哪行代码?

我的应用程序代码是:

/// <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.
    }
}

如何使用 C# 在 MVC 5 中停止执行进程

更改RedirectToControllers以返回 ActionResult,然后设置 filterContext.Result 。

//filterContext.Result = new RedirectResult("~/account/login");
filterContext.Result =  RedirectToControllers(ControllerHelper.Controller.ACCOUNT,  ControllerHelper.Controller.Action.ACCOUNT_LOGIN);

您是否尝试过使用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.
    }
 }