Preventing data forwarding when updating page (F5) in ASP.NET

Asked

Viewed 3,712 times

3

Guys, I have the following problem:

I have a web application made with ASP.NET and C#.

In this application I have a simple entry registration system.

After I register an item in the application, if I have the browser page updated (press F5), the browser opens a pop up asking to resend the form, consequently it generates a duplicate in the database.

This is the excerpt from the code where I register at the bank:

PgSqlConnection Myconn = new PgSqlConnection(connectionString);
        query = "INSERT INTO banco(a,b,c,d,e,f) VALUES (" + a + "," + b.SelectedValue + "," + c + "," + d + "," + e +"," + f.SelectedValue + ")";
        PgSqlCommand Command = new PgSqlCommand(query, Myconn);
        Myconn.Open();
        try
        {
            Command.ExecuteNonQuery();
        }
        catch (PgSqlException ex)
        {
            String myscript = "alert('Não foi possível executar esta ação')";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", myscript, true);
        }

How can I stop it?

To facilitate further research, please follow below the excerpt of the code with the solution of the problem:

    PgSqlConnection Myconn = new PgSqlConnection(connectionString);
    query = "INSERT INTO banco(a,b,c,d,e,f) VALUES (" + a + "," + b.SelectedValue + "," + c + "," + d + "," + e +"," + f.SelectedValue + ")";
    PgSqlCommand Command = new PgSqlCommand(query, Myconn);
    Myconn.Open();
    try
    {
        Command.ExecuteNonQuery();            
        Response.Redirect("estearquivo.aspx"); //isto soluciona o problema
    }
    catch (PgSqlException ex)
    {
        String myscript = "alert('Não foi possível executar esta ação')";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", myscript, true);
    }

2 answers

8


I already answered an equivalent question in PHP, but the same goes for any other language because what is in question is an HTTP request. Come on:

When you make a POST in a form, it is incorrect that you generate the listing (or a confirmation message, or anything else) in that POST reply. The point is that you redirect the user to another page, generating a GET right after this POST.

With this, an F5 would cause a GET on the next page and not a POST, preventing the form from being posted again.

  • I updated the post with an excerpt of my code, can show how it could change to fix this problem?

  • From what I understand, you render the return template more or less at the same stretch as when you try to insert it into your database. What you need to return is a 301 redirect to another page, rather than rendering a template.

  • @Rodrigorigotti Redirect 301 would be inappropriate in this case. A more appropriate code would be 303. Explanation: 301 indicates a permanent redirect, in fact, the browser would not access this address again, but the following. 303 indicates that the content is elsewhere, but the first address cannot be deleted. Fountain: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

4

This is a simple problem: when giving F5, and the last action was a POST, the browser forwards the POST. As @Rodrigo Rigotti said you can in the first POST direct with a 301 to another page, for example send to the list page of registered items.

The code you posted in your question is just the execution of the data entry, it is not in it that you need to change, but in the HTTP request control. If you want to redirect to another page after entering the data use the Response Redirect:

Response.Redirect("OutraPagina.aspx", false);

There is a detailed example at this link.

Or you can keep in viewstate from your page the type of user action and check the action before saving the data. See this another link with a detailed example.

I hope it helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.