39

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

tereško
  • 58,060
  • 25
  • 98
  • 150
evevffv
  • 446
  • 1
  • 4
  • 14

3 Answers3

17

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!

Community
  • 1
  • 1
technicallyjosh
  • 3,511
  • 15
  • 17
7

I am new to MVC but have found that I am using a custom class Authenticate and one of the properties returns a RedirectToRouteResult. This class is not a controller (or derived from it) so RedirectToAction is not available and I would use RedirectToRouteResult.

The Property looks like this:

    public RedirectToRouteResult NotLoggedInPage
    {
        get
        {
            return new RedirectToRouteResult(new RouteValueDictionary(new { action = "LoggedOut", controller = "Login" }));
        }
    }
Dawie Snyman
  • 87
  • 1
  • 1
0

It's almost the same but...
what happens when you work with a few custom routes? is an alternative that supports routes

with the second approach you work with the default route, but when you need use a specific route with 3 or 4 parameters, you can use the first approach and specified route name and with all parameters.

this kind of options you can find in helpers too, for example, a news paper site:

your project has two routes

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", 
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
            );

            routes.MapRoute(
                "NewsRoute", // Route name
                "News/{action}/{year}/{month}/{day}/{news}", 
                new { controller = "News", action = "show", year = 2013, month = 6, news = "start-new" } 
            );

the default route it's used for contents and special pages and the NewsRoute it's used for show and edit news

if you need build some url you must do it like this

@Url.Action("Home","Contact")

@Url.RouteUrl("NewsRoute", new RouteValueDictionary(new {action = "show", year = 2013, month = 6, news = "title news" }));

and the redirect it's the same way

Zach dev
  • 1,610
  • 8
  • 15
  • When u said 3-4 parameters and use 1st approach. But, you can set params in case of Approach 2 like this `return RedirectToAction("Fileresult", "ActionResultTypes", new { id = 10 });` – evevffv Jun 05 '13 at 17:34