Why HttpResponse.AppendHeader does not apply if Redirect is used?

  • Thread starter Thread starter newsgroup.poster
  • Start date Start date
N

newsgroup.poster

I need to set a custom http header before perform http redirection.

I do

response.AppendHeader("aCustomHeader", "aCustomValue")
response.Redirect("anUrl")

and my added header doesn't show up, it seems to be erased, if I
remove the redirect, it does.

Looking at Redirect documentation (http://msdn.microsoft.com/en-us/
library/t9dwyts4.aspx) it doesn't explain why the behaviour is
occuring.

How can I workarround this issue?

Am I forced to implement my own redirect?

Thanks,

Gauthier Segay
 
I need to set a custom http header before perform http redirection.

I do

response.AppendHeader("aCustomHeader", "aCustomValue")
response.Redirect("anUrl")

and my added header doesn't show up, it seems to be erased, if I
remove the redirect, it does.

Looking at Redirect documentation (http://msdn.microsoft.com/en-us/
library/t9dwyts4.aspx) it doesn't explain why the behaviour is
occuring.

How can I workarround this issue?

Am I forced to implement my own redirect?

Thanks,

Gauthier Segay

There is a clue in the documentation, under Exceptions:

HttpException A redirection is attempted after the HTTP
headers have been sent

So I think you're going to have to set the RedirectLocation property
and choose an appropriate status to send.

Damien
 
a Response.Redirect clears all content. you will need to write your own
redirect.

-- bruce (sqlwork.com)
 
Thanks to both for your replies, I've added a comment to the msdn
library

if anyone have pointers to some freely usable implementation of
redirect that allows more flexibility, that would be of great use.

In the mean time I've hacked something simpler for my limited usecase.
 
To do redirect you need to do 4 lines of code.

Request.Status = "302 Moved Permanently";
Request.StatusCode = 302;
Request.AddHeader("Location", sUrl);
rq.End();

George.

Thanks to both for your replies, I've added a comment to the msdn
library

if anyone have pointers to some freely usable implementation of
redirect that allows more flexibility, that would be of great use.

In the mean time I've hacked something simpler for my limited usecase.
 
Back
Top