Confusion between Redirect and RedirectToAction

Viewed 28588

I'm studying for a MS certificate (70-515).
I'm a confused with what I find online and what I read in a practice test.
A few questions on SO state that using a RedirectToAction is sending the browser a 302, thus causing it to change it's url in the address bar.

But this is a question from 1 of the practice tests:

QUESTION:

The MVC Home controller currently has only the default Index action. The relevant code is shown in the following code example.

public ActionResult Index()
{
    ViewData["Message"] = "Hello!";
    return View();
}

You need to create an action named FindID that displays the ID parameter entered as part of the path. If the path does not include an ID parameter, ASP.NET must process the Index action without changing the URL in the browser's address bar, and must not throw an exception. Which code segment should you use?

CORRECT ANSWER:

public ActionResult FindID(int? id)
{
    if (!id.HasValue)
        return RedirectToAction("Index");
    ViewData["Message"] = "ID is " + id.ToString();
    return View();
}

EXPLANATION:

You can use the RedirectToAction form of ActionResult to cause MVC to process a different action from within an action. MVC abandons the current action and processes the request as if the route had led directly to the action you redirect to. Essentially, this is equivalent to calling Server.Transfer in a standard ASP.NET application.

The Redirect ActionResult sends an "HTTP Error 302 - Found" response to the browser, which causes the browser to load the specified URL. This changes the address that appears in the address bar.

So:
- Does a RedirectToAction leave the URL in browser untouched?
- Does a Redirect change the URL in browser?
- Is the explanation of the practice test correct? From that I understand that RedirectToAction does NOT do a 302.

2 Answers
Related