ASP Response Object : Redirect Method

The Redirect method stops processing the current script and tries to connect the end user to a different URL. To accomplish this, the following explicit HTTP header is sent to the end user's browser:

  HTTP 1.1 302 Object Moved
  Location URL
Where URL is the parameter passed to the Redirect method. If, however, page content has already been sent to the end user and a proxy server lies between the server and end user, an error message can be generated. To prevent this, Response.Buffer should be set to True and Response.Clear should be called just before calling the Redirect method.

This method takes one mandatory parameter:

  • URL - the URL to send the end user to

Example:

<%
  Response.Buffer = True

  'any amount of code, response.writes, etc...

  Response.Clear
  Response.Redirect("http://www.everything2.com/")
%>
This will redirect the user to Everything, regardless of the code in between1.

Tips & Hints

Transfer Instead of Redirecting for Internal Pages
With the introduction of ASP 3.0 and IIS 5.0 came some new functionality which largely makes the use of the Redirect method obsolete for internal pages (pages on the same server). You should now use Server.Transfer instead of the Redirect method to send users to different pages on your system.

The reason is that a redirect forces a new page request, resulting in the browser having to connect again to the web server and the server having to handle an extra request. Server.Transfer, on the other hand, transfers execution to a different ASP page on the same server, avoiding the extra browser-to-server connection and resulting in better performance.


Back to the ASP Response Object
Back to ASP Objects
1 As long as Response.End or Server.Transfer aren't called.

Resources:
http://www.devguru.com/
http://msdn.microsoft.com/asp/

All code is my own (and is a seventh cousin of EDB's).

Log in or register to write something here or to contact authors.