Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Response Header before Server.Transfer in Asp.Net?

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?

like image 502
shashi Avatar asked Dec 26 '22 21:12

shashi


1 Answers

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

like image 100
Andre Calil Avatar answered Mar 15 '23 09:03

Andre Calil