Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion between Redirect and RedirectToAction

Tags:

asp.net-mvc

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.

like image 758
TweeZz Avatar asked Jun 25 '11 10:06

TweeZz


People also ask

What is the difference between return view and return redirect?

Return View doesn't make a new requests, it just renders the view without changing URLs in the browser's address bar. Return RedirectToAction makes a new request and the URL in the browser's address bar is updated with the generated URL by MVC.

Which is correct syntax for RedirectToAction?

RedirectToAction(String, String, RouteValueDictionary) Redirects to the specified action using the action name, controller name, and route values.

Is RedirectToAction a post or a get?

When you call RedirectToAction within a controller, it automatically redirects using an HTTP GET.

How redirect a specific view from controller in MVC?

You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is: return RedirectToAction("Index", model); Then in your Index method, return the view you want.


1 Answers

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.

This is incorrect.

Both the RedirectToRouteResult (RedirectToAction) and RedirectResult perform a 302 redirect, resulting in the URL in the browser changing.

To return the Index result without changing the url the code would actually be:

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

However, I would not recommended this approach. If I make a request to mysite.com/products/some-product and some-product does not exist, then I should inform the user of that with the relevant status code (also important for search engines).

If the sole purpose of your FindID action is to do something with the id parameter, then it should not be nullable/optional. This way the FindID action would not be invoked if an ID was not specified.

like image 148
Ben Foster Avatar answered Sep 19 '22 03:09

Ben Foster