Daily Archives: 02/10/2015

ASP.NET WebForms: Safe refresh after postback

It’s all too commons scenario in a Web Application, you initiate a postback by clicking a button (basically submitting a form), some action is performed, perhaps database is written to, all fine and good. And then you refresh the page. Or even page is refreshed for some purpose by a client-side JavaScript code. And the dreaded “resubmit” message appears, it differs from browser to browser, e.g. Firefox would say

“To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier”

And if you agree – the form is resubmitted again along with all the actions performed – not good.

This issue happens because the page is submitted via POST request and in order to refresh the page – POST request has to be resubmitted along with form action. And the solution is Post/Redirect/Get pattern. The idea is to take the page submitted via POST request and convert it into GET. In ASP.NET this can be achieved via simple Response.Redirect. You can redirect to another page or you can reload your current one:

Response.Redirect(Request.RawUrl)

This code redirects to original page URL, essentially reloading the from page server side. But now it’s a GET page, safe to refresh