user1464139
user1464139

Reputation:

Is RedirectToAction("Index", "Home") the same as return Redirect("~/home")

In my controller I would like to redirect. I have two choices:

if(User.Identity.IsAuthenticated)
{
    return RedirectToAction("Index", "Home");
}

or

if(User.Identity.IsAuthenticated)
{
    return Redirect("~/home");
}

In my routes I have:

     routes.MapRoute(
        "spa",
        "{section}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new { section = @"home|questions|admin" });

Can someone tell me if there is any difference in my choosing one redirect over the other.

Upvotes: 1

Views: 1947

Answers (1)

Ahmed ilyas
Ahmed ilyas

Reputation: 5822

It would be better to use the RedirectToAction because then at least you can specify which controller you are referring to and the routing etc.. may change where as if you do Redirect, you are most likely going to enter an invalid path (i.e more error prone than using RedirectToAction since it does the magic for you)

Remember, ASP.NET MVC is built on top of ASP.NET so try to take as much advantage of the goodness of MVC as you can. Will save you headaches in the long run!

Upvotes: 1

Related Questions