views:

263

answers:

5

I am working on a website that I inherited (ASP.NET and C#), and I noticed that in almost EVERY method in the code behind of the project pages (except some helper methods), the original author uses Response.Redirect() to redirect to a page (typically home.aspx, but not always).

What is the purpose of doing this? It seems unneeded to me - at least it doesn't appear to change anything the website is doing if I keep it in or remove it.

Thanks.

A: 

Without more information it's hard to be definitive.

However, if home.aspx is an empty page, it may be that the original author may have been trying to terminate the processing of the page early in an effort to prevent subsequent processing.

Normally, Response.Redirect() is used to end the response and inform the browser to navigate to a new page. However, if the browser has that page cached, it may not actually perform a trip to the server. I've seen some cases where developers do this as a way of short-circuiting subsequent processing.

It's also possible that the code is doing something crazy, like making home.aspx the main display page for all data - and using session state or cache to communicate changes across pages. Sadly, I've seen this done too.... sigh. Often this is done to deal with the user being able to multiply submit forms.

LBushkin
No, the home.aspx page is the home page (and it's definitely not empty - a little too full, in fact, but that's another issue).
JasCav
A: 

Redirects should really only be used when location is determined by something in the code behind. Redirects tend to cause ThreadAbortExceptions which are just further demand on a system when a simple href might be what the doctor ordered. Unless you can define some true architectural need for redirects, you might just want to begin phasing these things out.

Joel Etherton
A: 

It sends a response to the user agent/browser and tells it to redirect to the specified page. It can be put into any part of the code, but by default, the page will still execute to completion, then the redirect response will be set to the client...

It should only be needed at the last point in the code that you are running (generally)

jwwishart
In the code that I've looked at, the Response.Redirects are always the last part of the code (the very last line in a method inside the code-behind). But, many times, the redirect will be to a page that I am already on. This is what is confusing me.
JasCav
Thats right! it should "generally" be... Response.Redirect can be called at any point though from memory, and the redirect response will be sent to the browser after the page has fully finished rendering...If they were redirecting to the current page, then that is confusing!!! @womp's thought might be a reasonable guess!!!
jwwishart
A: 

ASP.NET Pages Post back to themselves, so some use the redirect method to open a new page. Use it when you need it. If you don't see a difference when you remove it. It might be the site uses links to navigate from one page to another, instead of doing it via the server.

Bless Yahu
+4  A: 

Response.Redirect() issues a 302 HTTP Redirect header to the browser, which causes the browser to request a new page from your web site.

If the author was using the POST-Redirect-GET pattern to stop the problem with users being able to hit the "refresh" button and repost forms, this might explain why it's used everywhere.

womp