Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use RedirectToAction and where to use RedirectToRouteResult?

Question

In which context, I can use RedirectToAction and where to use RedirectToRouteResult ?

I have two Action Methods like below.

Approach - 1

public class ActionResultTypesController : Controller {     public ActionResult Content()     {         return new RedirectToRouteResult(new RouteValueDictionary(                new { action = "Fileresult", controller = "ActionResultTypes" }));     }     public ActionResult Fileresult()     {         return View();     } } 

Approach - 2

I could write the same code like below as well. The only difference is that this time I used RedirectToAction in place of RedirectToRouteResult

public class ActionResultTypesController : Controller {     public ActionResult Content()     {         return RedirectToAction("Fileresult", "ActionResultTypes");     }     public ActionResult Fileresult()     {         return View();     } } 

Both piece of code have common Resultant

like image 375
evevffv Avatar asked Jun 05 '13 16:06

evevffv


People also ask

What is the usage of a RedirectToAction?

The RedirectToAction() Method This method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action. This acts just like as Response.

What is difference between RedirectToAction and RedirectToRoute?

RedirectToAction will return a http 302 response to the browser and then browser will make GET request to specified action. Show activity on this post. Ideally I would use RedirectToRoute for Action Links/Images and RedirectToAction in Controller's Action to redirect to another Controller's Action .

What is RedirectToRouteResult?

RedirectToRouteResult. RedirectToRouteResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header. Targets a registered route. It should be used when we want to redirect to a route.


1 Answers

There isn't much difference between the two when using within the controller like you have in your example.

They both ultimately achieve the same goal. However, RedirectToRouteResult() is mostly used in an action filter type scenario seen here. It's a little less friendly on the eyes when just using in your actions on controllers.

Both can achieve the same goal. The questions you need to ask yourself in most scenarios are really:

  1. Do I need the permanent redirect flag when using RedirectToRouteResult()?
  2. Do I want to write the extra code when using RedirectToRouteResult()?

If your answer is no or I don't know,

RedirectToAction("Action", "Controller", new { parameter = value }); 

is probably your best bet!

EDIT:

Here's some light on what RedirectToRouteResult is.

Reference to some MVC Redirects.

In this you will notice that RedirectToRouteResult is not something that you would normally call to return in an action. It is used as a return type for multiple RedirectToRoute calls. For instance, there are 2 calls you will see in that book. RedirectToRoute and RedirectToRoutePermanent.

They both return RedirectToRouteResult except, RedirectToRoutePermanent returns the result with the permanent redirect bool true. This returns a HTTP 301 status code.

Hope this helps!

like image 131
technicallyjosh Avatar answered Sep 21 '22 20:09

technicallyjosh