Confusion between Redirect and RedirectToAction

I am studying MS certificate (70-515).
I am confused by what I found on the Internet and what I read on a field trial.
A few SO state questions that use RedirectToAction are sent to browser 302, which causes the URL in the address bar to change.

But this is a question from 1 practical tests:

QUESTION:

Currently, only the default index action is valid on the MVC master. The corresponding code is shown in the following code example.

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

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

CORRECTED RESPONSE:

 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 ActionResult form to force MVC to handle another action from the action. MVC discards the current action and processes the request as if the route led directly to the action you are redirecting to. In essence, this is equivalent to calling Server.Transfer in a standard ASP.NET application.

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

So:
- Does RedirectToAction remove the URL in the browser intact?
- Does the redirect change the URL in the browser?
- Is the explanation of the practical test correct? From this, I understand that RedirectToAction does NOT do 302.

+12
asp.net-mvc
Jun 25 2018-11-11T00:
source share
2 answers

You can use the RedirectToAction ActionResult form to force MVC to handle another action from the action. MVC discards the current action and processes the request as if the route led directly to the action you are redirecting to. In essence, this is equivalent to calling Server.Transfer in a standard ASP.NET application.

This is not true.

Both RedirectToRouteResult (RedirectToAction) and RedirectResult both redirect 302, causing the browser URL to change.

To return the result of an index without changing the code, the code is actually:

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

However, I would not recommend this approach. If I make a request to mysite.com/products/some-product and some-product does not exist, then I must inform the user with the appropriate 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 null / optional. Thus, the FindID action will not be called if no identifier is specified.

+9
Jun 25 '11 at 11:01
source share

The documentation for the RedirectToAction method tells us that it sends a 302 response:

"Returns an HTTP 302 response to the browser, causing the browser to make a GET request for the specified action."

Studying the code in the dll shows that it returns a RedirectToRouteResult object, which causes a redirect, so the documentation is correct:

 protected internal virtual RedirectToRouteResult RedirectToAction(string actionName, string controllerName, RouteValueDictionary routeValues) { RouteValueDictionary routeValueDictionaries; if (this.RouteData == null) { routeValueDictionaries = RouteValuesHelpers.MergeRouteValues(actionName, controllerName, null, routeValues, true); } else { routeValueDictionaries = RouteValuesHelpers.MergeRouteValues(actionName, controllerName, this.RouteData.Values, routeValues, true); } return new RedirectToRouteResult(routeValueDictionaries); } 

The correct answer to the test question is to use a different view:

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

This will use the Index view instead of the FindID , which is returned by a pointless call to View() .

+9
Jun 25 2018-11-11T00:
source share



All Articles