05
OctASP.NET MVC - View() vs RedirectToAction() vs Redirect() Methods
View() vs RedirectToAction() vs Redirect() Methods: An Overview
In this article, I am going to discuss View, Redirect, and RedirectToAction in the ASP.NET MVC Application. The MVC has different types of Action Results. Also, each action result returns a different format of the output. As a coder, we need to use different action results to get the expected output.
There are many ways to return or render a view in ASP.NET MVC. Many developers got confused about when to use View(), RedirectToAction(), Redirect() and RedirectToRoute() methods. In this article, In this MVC tutorial, I would like to explain the difference between the "View()" and "RedirectToAction()", "Redirect()" and "RedirectToRoute()" methods.
The View() Method:
This method generates the HTML markup to be displayed for the specified view and sent to the browser. This acts just like a Server.Transfer() method in ASP.NET WebForm.
Example of View() Method:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string Name)
{
ViewBag.Message = "Hi, Dot Net Tricks";
//Like Server.Transfer() in Asp.Net WebForm
return View("MyIndex");
}
public ActionResult MyIndex()
{
ViewBag.Msg = ViewBag.Message; // Assigned value : "Hi, Dot Net Tricks"
return View("MyIndex");
}
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string Name)
{
ViewBag.Message = "Hi, Dot Net Tricks";
//Like Server.Transfer() in Asp.Net WebForm
return MyIndex();
}
public ActionResult MyIndex()
{
ViewBag.Msg = ViewBag.Message; // Assigned value : "Hi, Dot Net Tricks"
return View("MyIndex");
}
What happens if we call the action method directly like return MyIndex()? As we see in the second window, It is simply a method call that returns a rendered view specified in the MyIndex() action method.
The RedirectToAction() Method
This method redirects to a specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and makes a new request for the specified action. This acts just like a Response.Redirect() in ASP.NET WebForm.
Moreover, RedirectToAction constructs a redirect URL to a specific action/controller in your application and uses the routing table to generate the correct URL. RedirectToAction causes the browser to receive a 302 redirect within your application and gives you an easier way to work with your route table.
Example of RedirectToAction() Method:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string Name)
{
ViewBag.Message = "Hi, Dot Net Tricks";
//Like Response.Redirect() in Asp.Net WebForm
return RedirectToAction("MyIndex");
}
public ActionResult MyIndex()
{
ViewBag.Msg = ViewBag.Message; // Assigned value : Null
return View("MyIndex");
}
The Redirect() Method
This method redirects to a specified URL instead of rendering HTML. In this case, the browser receives the redirect notification and makes a new request for the specified URL. This also acts like a Response.Redirect() in Asp.Net WebForm. In this case, you have to specify the full URL to redirect.
Moreover, Redirect also causes the browser to receive a 302 redirect within your application, but you must construct the URLs yourself.
Example of Redirect() Method:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string Name)
{
ViewBag.Message = "Hi, Dot Net Tricks";
//Like Response.Redirect() in Asp.Net WebForm
return Redirect("Home/MyIndex");
}
public ActionResult MyIndex()
{
ViewBag.Msg = ViewBag.Message; // Assigned value : Null
return View("MyIndex");
}
The RedirectToRoute() Method
This method looks up the specified route into the Route table defined in global.asax and then redirects to that controller/action defined in that route. This also makes a new request like RedirectToAction().
Defining Route
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"MyRoute", // Route name
"Account/", // URL
new { controller = "Account", action = "Login"} // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "MyIndex", id = UrlParameter.Optional } // Parameter defaults
);
}
HomeController
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string Name)
{
return RedirectToRoute("MyRoute");
}
AccountController
public ActionResult Login()
{
return View();
}
Important Note
The View() method doesn't make new requests, it just renders the view without changing URLs in the browser's address bar.
The RedirectToAction() method makes new requests and the URL in the browser's address bar is updated with the generated URL by MVC.
The Redirect() method also makes new requests and the URL in the browser's address bar is updated, but you have to specify the full URL to redirect
Between RedirectToAction() and Redirect() methods, the best practice is to use RedirectToAction() for anything dealing with your application actions/controllers. If you use Redirect() and provide the URL, you'll need to modify those URLs manually when you change the route table.
RedirectToRoute() redirects to a specific route defined in the Route table.
Summary:
I hope you will enjoy the tips and tricks while programming with ASP.NET MVC. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome.