Why should I use RedirectToAction?

Is there a difference between:

public ActionResult logOff() { FormsAuth.SignOut(); return RedirectToAction("index", "Home"); } 

and

 public ActionResult logOff() { FormsAuth.SignOut(); return index(); } 
+4
source share
2 answers

Yes.

With RedirectToAction() your users will be redirected to the Index page (this is what they will see in the address bar of the browser). Simply returning the result of your index() method instead fills the current page ( LogOff ?) LogOff contents of another page.

In this case, maybe there is no difference, but if your action follows some logic, then you may have problems when users simply refresh the page.

+8
source

Take a look at Publish / Redirect / Get Template

When a web form is sent to the server through a POST HTTP request, a web user who is trying to update the server’s response to certain user agents may invoke the contents of the original POST HTTP request for resubmission, which may lead to undesirable results, such as duplicating a purchase in The internet. To avoid this problem, many web developers use the PRG 1 template - instead of returning the web page directly, the POST operation returns a redirect command. The HTTP 1.1 specification introduced the HTTP response code 303 to ensure that in this situation the web browser browser can safely update the server response without triggering the initial HTTP POST request again. However, most of the common commercial applications used today (new and old similar) still continue to provide HTTP 302 responses in these situations. Using HTTP 301 is usually avoided because HTTP-1.1-compatible browsers do not convert the method to GET after receiving HTTP 301, as is usually done for HTTP 302. [2] However, HTTP 301 may be preferred in cases where POST parameters are undesirable. to be converted to GET parameters, and thus logged. The PRG template cannot address each scenario of a duplicated presentation form. Some well-known duplicate forms that PRG cannot solve: if the web user is updated before it is completed due to server delay, which leads to duplication of the HTTP POST request in certain user agents.

This is one of the most common cases where a redirect pattern is used with HTTP messages in asp.net mvc.

+1
source

All Articles