Saturday, May 20, 2006

Redirect to Self (ASP.NET)

{

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

Interestingly enough, if you have a named anchor in the form of <a name=#test, this is not exposed by any of these methods! (You'll see how I know a post or so from now, but suffices to say that you can still get this information from the client side by parsing the window.location object:

alert(window.location);

}

11 comments:

sonicaab said...

Thanks for posting this, it saved me a lot of time searching MSDN.

Sjoerd Perfors said...

Thanks for the post, just what i needed

Stefan V. Ott said...

Exactly what I needed! Thanks bud!

fahimeh said...

Thank you! this was such a helpful for me!

fahimeh said...

Thank you! this was very useful for me

fahimeh said...

Thank you! this was such a helpful for me!

Fanda said...

Thank you for this information. Saves me a lot of time.

Unknown said...

It is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand meta developer concepts. As a beginner in Dot Net programming'.Thanks for your informative article.
dot net training in velachery |
dot net training in chennai

Unknown said...

Great post! Thanks for sharing with us.

Angularjs Training in Chennai | Web Designing Training in Chennai

Unknown said...

Great blog! Thanks for giving such valuable information, this is unique one. Really admired

Dot Net Training in Chennai

DedicatedHosting4u said...



This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to article very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
DedicatedHosting4u.com