How To Use Response.Redirect in a Server Script

Last reviewed: December 11, 1997
Article ID: Q159402
1.00 WINDOWS NT kbcode kbprb kbhowto

The information in this article applies to:

  • Microsoft Active Server Pages, version 1.0 on the following platforms: Alpha, MIPS, Power PC, x86

SUMMARY

When trying to use the Response.Redirect method in a server-side script, the following error can occur when the page is accessed:

   Response
   Headers already written to client.

MORE INFORMATION

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.

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
   %>

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.

REFERENCES

For the latest Knowledge Base articles and other support information on Visual InterDev and Active Server Pages, see the following page on the Microsoft Technical Support site:

   http://support.microsoft.com/support/vinterdev/


KBCategory: kbcode kbprb kbhowto
KBSubcategory: AXSFHTML
Additional reference words: 1.00 kbdsi
Keywords : AXSFHTML kbcode kbhowto kbprb
Version : 1.00
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 11, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.