Thursday, June 10, 2010

ASP.NET Redirect to Self


It's easy to reset a page to it's initial state in ASP.NET by redirecting to itself. Here are 3 ways you can do it:



1. Response.Redirect(Request.Path);

In which the path to the request is presented in the following form: /MyApp/MyFile.aspx



2. Response.Redirect(Request.RawUrl);

In which not only is the path exposed, but also any querystring parameters like:
/MyApp/MyFile.aspx?foo=bar



3. Response.Redirect(Request.Url.ToString());

In which not only is the path and querystring parameters exposed, but made available as an absolute reference in the form:
http://MyServer/MyApp/MyFile.aspx?foo=bar

1 comment:

  1. Thanks! Just used this today!

    ReplyDelete

Swidget