Rodrigo Manguinho
Rodrigo Manguinho

Reputation: 1411

Why should i use RedirectToAction?

Is there any difference between:

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

And:

public ActionResult logOff()
{
    FormsAuth.SignOut();
    return index();
}

Upvotes: 4

Views: 365

Answers (2)

archil
archil

Reputation: 39501

Take a look at Post/Redirect/Get Pattern

When a web form is submitted to a server through an HTTP POST request, a web user that attempts to refresh the server response in certain user agents can cause the contents of the original HTTP POST request to be resubmitted, possibly causing undesired results, such as a duplicate web purchase. To avoid this problem, many web developers use the PRG pattern1 — instead of returning a web page directly, the POST operation returns a redirection command. The HTTP 1.1 specification introduced the HTTP 303 response code to ensure that in this situation, the web user's browser can safely refresh the server response without causing the initial HTTP POST request to be resubmitted. However most common commercial applications in use today (new and old alike) still continue to issue HTTP 302 responses in these situations. Use of HTTP 301 is usually avoided because HTTP-1.1-compliant browsers do not convert the method to GET after receiving HTTP 301, as is more commonly done for HTTP 302.[2] However, HTTP 301 may be preferred in cases where it is not desirable for POST parameters to be converted to GET parameters and thus be recorded in logs. The PRG pattern cannot address every scenario of duplicate form submission. Some known duplicate form submissions that PRG cannot solve are: if a web user refreshes before the initial submission has completed because of server lag, resulting in a duplicate HTTP POST request in certain user agents.

This is one of the most common cases when redirect pattern is used with HTTP Posts in asp.net mvc too.

Upvotes: 1

Adriano Repetti
Adriano Repetti

Reputation: 67090

Yes.

With RedirectToAction() your users will be redirected to Index page (that's what they'll see on the browser address bar). Simply returning the result of your index() method instead will fill the current page (LogOff?) with the content of the other page.

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

Upvotes: 8

Related Questions