Please enable Javascript to correctly display the contents on Dot Net Tricks!

return View() vs return RedirectToAction() vs return Redirect() vs return RedirectToRoute()

Posted By : Shailendra Chauhan, 11 Mar 2013
Updated On : 25 Feb 2014
Total Views : 238,803   
Support : MVC4 & 3
 

There are different ways for returning/rendering a view in MVC Razor. Many developers got confused when to use return View(), return RedirectToAction(), return Redirect() and return RedirectToRoute(). In this article, I would like to explain the difference among "return View()" and "return RedirectToAction()", "return Redirect()" and "return RedirectToRoute()".

return View()

This tells MVC to generate HTML to be displayed for the specified view and sends it to the browser. This acts like as Server.Transfer() in Asp.Net WebForm.

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");
}

What happens if we call the action method directly like return MyIndex(). It is simply a method call which returns a rendered view that is specified in MyIndex() action 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 MyIndex(); 
}

public ActionResult MyIndex()
{
 ViewBag.Msg = ViewBag.Message; // Assigned value : "Hi, Dot Net Tricks"
 return View("MyIndex");
}

return RedirectToAction()

This tells MVC to redirect to specified action instead of rendering HTML. In this case, browser receives the redirect notification and make a new request for the specified action. This acts like as Response.Redirect() in Asp.Net WebForm.

Moreover, RedirectToAction construct a redirect url to a specific action/controller in your application and use the route table to generate the correct URL. RedirectToAction cause the browser to receive a 302 redirect within your application and gives you an easier way to work with your route table.

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");
}

return Redirect()

This tells MVC to redirect to specified URL instead of rendering HTML. In this case, browser receives the redirect notification and make a new request for the specified URL. This also acts like as Response.Redirect() in Asp.Net WebForm. In this case, you have to specify the full URL to redirect.

Moreover, Redirect also cause the browser to receive a 302 redirect within your application, but you have to construct the URLs yourself.

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");
}

return RedirectToRoute()

This tells MVC to look up the specifies route into the Route table that is is defined in global.asax and then redirect to that controller/action defined in that route. This also make a new request like RedirectToAction().

Defined 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();
}

Note

  1. Return View doesn't make a new requests, it just renders the view without changing URLs in the browser's address bar.

  2. Return RedirectToAction makes a new requests and URL in the browser's address bar is updated with the generated URL by MVC.

  3. Return Redirect also makes a new requests and URL in the browser's address bar is updated, but you have to specify the full URL to redirect

  4. Between RedirectToAction and Redirect, 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.

What do you think?

I hope you will enjoy the tips and tricks while programming with MVC Razor. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

 
Recommended for you
 
About the Author
Shailendra Chauhan

Shailendra Chauhan is an Entrepreneur, Author, Architect, and Corporate Trainer. He has rewarded as Microsoft MVP for his exceptional contributions in Microsoft Visual Studio and Development Technologies.

With more than 7 years in hand experience Shailendra Chauhan is a polymath in the domains of Microsoft .NET technologies and an array of other technologies including JavaScript, AngularJS, Node.js, Ionic and NoSQL Databases to name but a few.

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers , ASP.NET MVC Interview Questions and Answers , AngularJS Interview Questions and Answers and LINQ Interview Questions and Answers. Furthermore he is a technical reviewer for book on ASP.NET MVC 4 Mobile App Development. Know more...
 
Free Interview Books
 
28 JUL
NodeJS Development (online)

Thursday 06:00 AM-7:30 AM IST

More Details
25 JUL
ASP.NET MVC with AngularJS Development (online)

Mon-Fri     (07:30 AM-09:00 AM IST)

25 JUL
AngularJS Development (online)

Mon-Fri     09:00 PM-10:30 PM IST

24 JUL
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun     09:30 AM-11:00 AM

24 JUL
AngularJS Development (offline)

Sat,Sun     (08:00 AM-09:30 AM)

2 JUL
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun     (03:00 PM-05:00 PM IST)

27 JUN
ASP.NET MVC with AngularJS Development (online)

Mon - Fri     (07:00 AM-09:00 AM IST)

BROWSE BY CATEGORY
 
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 
+