Tuesday, November 07, 2006

Errors When Trying to do Response.Redirect

Ever tried to do Response.Redirect and get the following error?
Response object error 'ASP 0159: 80004005'
Header Error. The HTTP headers are already written to the client browser.
Any HTTP header modifications must be made before writing page content.
When calling response.redirect, you have to execute it BEFORE any client-side code, including the opening <html> tag.

This usually isn't a problem in IIS 6 (Server 2003) because in IIS 6 the default setting for Response.Buffer is True by default. This means the server holds the page content until it's all ready to go and then sends it to the client. If Response.Buffer is set to True, you can call Response.Redirect anytime before the page is complete and it will work.

In older versions of IIS, Response.Buffer is set to False by default. This means content gets sent to the client as the page is executing. In this scenario, you'll get an error if you try to execute Response.Redirect anytime after content has been sent.

The fix? If you're in a version of IIS older than IIS 6, set Response.Buffer = True at the top of the page.

Another error you can get when using Response.Redirect is Object Moved. One way to prevent this from happening is using Response.Clear first (note that buffering must be enabled). At the top of the page:
Response.Buffer = true
Before you call Response.Redirect:
Response.Redirect "http://www.domain.com"

No comments: