Saturday, 7 September 2013

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

Here some points which elaborate difference between abouve signatures....

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, welcome to Nihar's Blog !! ";    //Like Server.Transfer() in Asp.Net WebForm    
    return View("MyIndex");
}
 public ActionResult MyIndex()
   
   ViewBag.Msg = ViewBag.Message;     // Assigned value : "Hi, Welcome to Nihar's blog"   
    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.

[HttpPost]
public ActionResult Index(string Name)
{
     ViewBag.Message= "Hi, Welcome to Nihar's blog"
    //Like Server.Transfer() in Asp.Net WebForm
      return 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.

  public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(string Name)
{
    ViewBag.Message = "Hi, Welcome to Nihar's Blog";
    //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.

  public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(string Name)
{
    ViewBag.Message = "Hi, Welcome to Nihar's Blog!!";
    //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().

In global.asax 
     
     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
    );
    }

    In Controller :

public ActionResult Index()
 {
     return View();
 }

[HttpPost]
public ActionResult Index(string Name)
 {
        return RedirectToRoute("MyRoute");
 }
 
For this article I refer  Pro ASP.Net MVC 4 .
( For ref : http://www.amazon.com/Pro-ASP-NET-MVC-Adam-Freeman/dp/1430242361 )

No comments:

Post a Comment