4

I have a page where based on certain conditions I am either doing a Response.Redirect or Server.Transfer. Now I want to add a header for both the cases. So I am doing the following

    Response.AddHeader("Vary", "User-Agent");

    if (condition) 
    {
        Server.Transfer(redirectUrl);
    }
    else
    {
        Response.Redirect(redirectUrl);
    }

Now, when the code goes via Server.Transfer code path, the Vary header is set to * whereas when it goes via Response.Redirect the header is correctly set to User-Agent.

Why does this happen and how can I set the Response Header to be same for both the cases?

shashi
  • 4,616
  • 9
  • 50
  • 77

3 Answers3

6

when you call Server.Transfer, the Response object of the current page will be replaced by the Response object of the target page (which is the Response that will actually be sent to the user). So, if you want to set this specific header attribute, you must do it on the target page.

If it's conditional, maybe you can use a HttpContext.Items property, that is set on the first page and read on the second.

Regards

Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
Andre Calil
  • 7,652
  • 34
  • 41
  • 2
    I think HttpContext.Items is probably more appropriate than Session in this case. Session will be persisted across requests whereas Items will be cleaned up once the request has completed. – Dean Ward Jul 10 '12 at 16:13
  • @DeanWard You got point! Feel free to edit the answer regarding this improvement. – Andre Calil Jul 10 '12 at 16:16
4

Andre is right that the Response object is replaced as part of Server.Transfer. If you want to make the page you're transferring to agnostic of the parent you can probably whack the information into HttpContext.Items and then use an IHttpModule to extract the information and configure the header appropriately. Something like this would probably do the job...

Items.Add(VaryHttpModule.Key, "User-Agent");

if (condition) 
{
    Server.Transfer(redirectUrl);
}
else
{
    Response.Redirect(redirectUrl);
}

public class VaryHttpModule : IHttpModule
{
    public const string Key = "Vary";

    public void Init(HttpApplication context)
    {
        context.PostRequestHandlerExecute +=
            (sender, args) =>
                {
                    HttpContext httpContext = ((HttpApplication)sender).Context;
                    IDictionary items = httpContext.Items;
                    if (!items.Contains(Key))
                    {
                        return;
                    }

                    object vary = items[Key];
                    if (vary == null)
                    {
                        return;
                    }

                    httpContext.Response.Headers.Add("Vary", vary.ToString());
                };
    }

    public void Dispose()
    {
    }
}

Cheers!

Dean Ward
  • 4,793
  • 1
  • 29
  • 36
0

Add header in Source.aspx and don't Change the header in Destination.aspx page.

If you want to display the result page as html then you should add header content-type as text/html

jafarbtech
  • 6,842
  • 1
  • 36
  • 55