ASP.NET MVC - View() vs RedirectToAction() vs Redirect() Methods

ASP.NET MVC - View() vs RedirectToAction() vs Redirect() Methods

04 Apr 2024
Intermediate
770K Views
6 min read

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

  1. The View() method doesn't make new requests, it just renders the view without changing URLs in the browser's address bar.

  2. The RedirectToAction() method makes new requests and the URL in the browser's address bar is updated with the generated URL by MVC.

  3. 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

  4. 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.

  5. 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.

Unlock the next level of MVC:

FAQs

Q1. What is the purpose of the RedirectToAction method in an ASP.NET MVC controller?

It sends users to either the action method of a different controller or another action method within the same controller

Q2. How will you navigate from one view to another view in MVC?

It using App. transferPage method.

Q3. How to return two views in MVC?

Create another class that will hold Userprofile and Employee, now write the below code to controller that will return MergeModel class to view.
Share Article
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this