In which context, I can use RedirectToAction
and where to use RedirectToRouteResult
?
I have two Action Methods like below.
public class ActionResultTypesController : Controller { public ActionResult Content() { return new RedirectToRouteResult(new RouteValueDictionary( new { action = "Fileresult", controller = "ActionResultTypes" })); } public ActionResult Fileresult() { return View(); } }
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(); } }
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.
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 .
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.
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:
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With