Urgent! Please help: Cannot redirect after http headers have been sent

A

Andreas Klemt

Hello,

I get this error Message "cannot redirect after http headers have been sent"
when I do this
response.redirect ("home.aspx")

How can I find out with vb.net if already a http header has been sent like

If response.IsSentHttpHeader then
....
End If.

Thanks for any help in advance!
Andreas Klemt
 
B

bruce barker

headers come before a content. if you turn off buffering and do a
Response.Write, or do a Respinse.Flush, its too late to change a header
because they have already been sent to the client.

-- bruce (sqlwork.com)
 
J

Joe Fallon

Ok.

But once you find yourself in this situation,
what can you do about it?

I end up in my Global error handler and then get another error when trying
to re-direct to my custom error page.

I just want to "end" the Response and go somewhere else.
Reponse.End does not work.

Any ideas?
 
A

Andreas Klemt

Hello Charles,

I do a response.write and a response.flash in my Page_Load.
But I would like to find out in my Page_PreRender if a Header is already
sent because I am not always doing a response.write in my Page_Load.

Is there a way to find it out like response.IsHeaderSent or something like
that?

Many thanks for any help.
Andreas
 
C

Chandra Sekhar

Just do a Response.ClearContent prior to your redirect. It should work.

Sekhar.
 
J

Jens Christian Mikkelsen

Andreas Klemt said:
I do a response.write and a response.flash in my Page_Load.
But I would like to find out in my Page_PreRender if a Header is already
sent because I am not always doing a response.write in my Page_Load.

Is there a way to find it out like response.IsHeaderSent or something like
that?

Sorry for going for the obvious, but have you considered using a boolean
variable?

May or may not be the "neatest" solution, but if you urgently need a
work-around...

/Jens
 
Joined
Aug 30, 2006
Messages
7
Reaction score
0
Why do we get this error? Remember that a normal page is sent with "200 OK" in its response headers, and a redirect is "302 Object moved". When IIS hits the first piece of HTML output;

<HTML>

it deduces that this is a standard "200 OK" page and that the browser should be sent output as it is generated. So it sends this;

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Mon, 19 Mar 2001 15:07:44 GMT
Connection: close
Content-Length: 134
Content-Type: text/html
Set-Cookie: ASPSESSIONIDQQGQQJWO=OMCJFABDNCDLLBKAPNHJBKHD; path=/
Cache-control: private

<HTML>


As more HTML is generated it is also sent to the browser to be displayed. When it hits the response.redirect command it has a problem. In order to re-direct it needs to send a "302 Object moved" header but it can't as it has already sent a "200 OK" header, thus the error about not being able to modify the headers.


Solution: Delete the line where you have the response.flush() written...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top