Redirection outside controller from another method

Zeeshan Dar 41 Reputation points
2022-11-10T12:33:04.88+00:00

I have several ActionResults where I implemented below condition. I want to move this complete if condition to a separate method and call it where needed. Please advice

> public async Task<ActionResult> Status(int Id)  
> {  
>       if (Id <= 0)  
>       {  
>           TempData["Errors"] = "Invalid data";  
>   
>           return RedirectToAction("Error", "Dashboard");  
>       } ....  
>  }  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,162 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2022-11-11T05:43:08.267+00:00

    Hi @Zeeshan Dar ,

    I want to move this complete if condition to a separate method and call it where needed.

    As we all know, the RedirectToAction() method will return a RedirectToRouteResult, so, if you want to create a separate method, the return data could be a RedirectToRouteResult. Like this:

        public IActionResult Status (int Id)  
        {   
            return RedirectToActionHandle(Id);  
        }  
    
        public RedirectToActionResult RedirectToActionHandle(int id)  
        {  
            if (id <= 0)  
            {  
                TempData["Errors"] = "Invalid data";  
                return RedirectToAction("Index");  
            }  
            else    
            {  
                TempData["Errors"] = "Invalid data";  
                return RedirectToAction("Index2");  
            }   
        }  
    

    Besides, in the if statement or custom method, you might return different action result, such as ViewResult, JsonResult and so on. So, in the custom method, the return data type, you can use IActionResult. like this:

        public IActionResult Status (int Id)  
        {   
            return CustomHandle(Id);  
        }  
    
        public IActionResult CustomHandle(int id)  
        {  
            if (id <= 0)  
            {  
                TempData["Errors"] = "Invalid data";  
                return RedirectToAction("Index");  
            }  
            else if(id== 2)  
            {  
                TempData["Errors"] = "Invalid data";  
                return RedirectToAction("Index2");  
            }  
            else  
            {  
                return View("Status");  
            }  
        }   
    

    And there also has another method, you can create ActionModel which contains the ActionName, ControllerName and RouteData property, in the custom method and the if statement, you can create an ActionModel instance and set the ActionName, ControllerName and the RouteData. Then return this model to the Status method, and then based on the ActionModel to redirect to related action method.

    259347-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,686 Reputation points
    2022-11-10T17:48:42.137+00:00
    0 comments No comments