Help and Support
 

powered byLive Search

How to use Response Redirect in a server script

Article ID:159402
Last Review:October 26, 2007
Revision:5.2
This article was previously published under Q159402
On This Page

SUMMARY

The Redirect method of the Response object operates by sending a header to the client. This header causes the client to look to another URL location specified in the header. Because a header must come at the beginning of a document, it is not possible to place the Redirect method in a document with HTML code preceding it.

To work around this behavior you can use the buffering capabilities of the Response object. In doing this you can output HTML code into the buffer until you reach a point where you use the Redirect method. If at this point you need to redirect to another page, you clear the buffer and then issue the Response.Redirect.

Back to the top

Example error


When trying to use the Response.Redirect method in a server-side script, the following error can occur when the page is accessed:
Response object error 'ASP 0156 : 80004005
Header Error
/<page.asp>, line 9
The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.

Back to the top

Example ASP

The following example Active Server Pages (ASP) code demonstrates this concept:
   <%
   ' Begin buffering the HTML
   ' Note this MUST happen before the initial <HTML> tag.
   Response.Buffer = True
   %>
   <HTML>
   <BODY>
   HTML code before potential redirect.<P>

   <%
   ' Change the following line as appropriate for your script
   If 1 = 1 Then
      Response.Clear
      Response.Redirect "filename.asp"
   End If
   %>
				

Back to the top

Code to be added

Use the following additional HTML code after the redirect:
   <%
   ' The following causes the HTML to actually be sent to the client.
   ' Up to this point, no HTML has actually been downloaded to the client
   ' browser.
   Response.End
   %>
   </BODY>
   </HTML>
				
The above example always redirects to the file named Filename.asp.

Note Setting the Response.Buffer = True is not necessary in Windows 2000 because it is True by default.

Back to the top

REFERENCES

For the latest Knowledge Base articles and other support information on Visual InterDev and Active Server Pages, visit the following page on the Microsoft Technical Support site:
http://support.microsoft.com/ph/3022 (http://support.microsoft.com/ph/3022)


Back to the top


APPLIES TO
Microsoft Active Server Pages 4.0
Microsoft Internet Information Server 4.0
Microsoft Internet Information Services 5.0

Back to the top

Keywords: 
kbproductlink kbaspobj kbcode kbcodesnippet kberrmsg kbhowto kbhowtomaster kbsample kbscript KB159402

Back to the top

Article Translations

 

Related Support Centers

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.