Redirect, RedirectToRoute and RedirectToAction in ASP.NET MVC

Redirect, RedirectToRoute and RedirectToAction in ASP.NET MVC

In this article, I am going to discuss Redirect, RedirectToRoute, and RedirectToAction in the ASP.NET MVC Application. The ASP.NET MVC has different types of Action Results. Each action result returns a different format of the output. As a programmer, we need to use different action results to get the expected output. Action Results return the result to view the page for the given request. 

Note: We are going to work with the same example that we started in View Result and Partial View Result in ASP.NET MVC article and continue in File Result, Content Result, Empty Result, JavaScript Result, and JSON Result in MVC article of this MVC article series.

Redirect Result in ASP.NET MVC

Suppose, you want to redirect to a specific URL, then you need to use the Redirect method and this method takes the URL to recirect. For example, suppose, we want to redirect to the URL: https://dotnettutorials.net, then we need to use the Redirect method as shown in the below code.

public class HomeController : Controller
{
    public RedirectResult Index()
    {
        return Redirect("https://dotnettutorials.net");
    }
}

This works great for redirecting to outside sites from the current application, but not for redirecting to other pages within the same application. For that, we can use RedirectToRouteResult. Redirect result is returning the result to a specific URL. It is rendered to the page by URL. If we give the wrong URL, it will show 404-page errors.

RedirectToRoute Result in ASP.NET MVC

The RedirectToRouteResult is used whenever we need to go from one action method to another action method within the same or different controller in ASP.NET MVC Application. For example, in the below code, we are redirecting to Home Controller, About action method from the Index action method of Home Controller.

public class HomeController : Controller
{
    public RedirectToRouteResult Index()
    {
        return RedirectToRoute(new { controller = "Home", action = "About" });
    }
}

That’s not very friendly though. There’s a better way, an overload of this helper called RedirectToAction:

RedirectToAction Result in ASP.NET MVC

The RedirectToAction Result in ASP.NET MVC is returning the result to a specified controller and action method. Controller name is optional in RedirectToAction method. If not mentioned, the Controller name redirects to a mentioned action method in the current Controller. Suppose the action name is not available but mentioned in the current controller, then it will show a 404 error page.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("Login", "Account");
    }
}

In the next article, I am going to discuss HttpStatusCodeResult, HttpUnauthorizedResult, and HttpNotFoundResult in ASP.NET MVC application, Here, in this article, I try to explain Redirect Result, RedirectToRoute Result, and RedirectToAction Result in ASP.NET MVC application step by step with a simple example. I hope you enjoy this Redirect Result, RedirectToRoute Result, and RedirectToAction Result in the ASP.NET MVC article.

1 thought on “Redirect, RedirectToRoute and RedirectToAction in ASP.NET MVC”

  1. It’s very good tutorial for beginner to professional. I just want to request you to upload explaination for all actionresults as few of them in list has no explainations

Leave a Reply

Your email address will not be published. Required fields are marked *