簡體   English   中英

如何使用C#在MVC 5中停止執行過程?

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

我需要在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.
    }
}

更改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.
    }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM