Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have two pages in ASP.NET Web Application(Page1.aspx and Page2.aspx). I am adding 'USER' as Http Response Header on Page1.aspx and then redirecting to Page2.aspx. Now on Page2.aspx, I need to read Http Header 'USER' and want to display this in label control.


Http Response Header 'USER' is successfully added for Page1.aspx(Checked on Fiddler) but the same not appearing in the Http Request Header of Page2.aspx.

What I have tried:

Page1.aspx....

protected void Button1_Click(object sender, EventArgs e)
{
    String URL = "Page2.aspx";
    HttpContext.Current.Response.AddHeader("USER", "admin");
    Response.Redirect(URL);
}


Page2.aspx....

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!IsPostBack)
        {
            if (HttpContext.Current.Request.Headers.Get("USER") != null)
            {
                string tUserName = Convert.ToString(HttpContext.Current.Request.Headers.Get("USER"));
                if (tUserName != null || tUserName.ToString() != "")
                {
                    Label1.Text = tUserName;
                }
            }
            Label2.Text = HttpContext.Current.Request.Headers.Get("X-AspNet-Version");
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }

}
Posted
Updated 13-Oct-17 0:02am
v2
Comments
F-ES Sitecore 13-Oct-17 5:16am    
The header you add to the response is not going directly to page2, it is being sent to the browser and .net adds another header that lets the browser know where to redirect to. The browser then makes a request to page2 the same way it does another other page, it doesn't copy the headers across to the new request.

So basically you can't use headers to do this, you should store the data you want to transfer in the Session instead.
Richard Deeming 13-Oct-17 8:29am    
Sounds like the solution to me. :)
Thanks7872 13-Oct-17 7:28am    
I am not getting how on earth do you think that this is the proper way to "pass" data between pages?
A_Griffin 15-Oct-17 11:44am    
This belongs in "Coding horrors".... not just passing any old data either, but an admin token!
Karthik_Mahalingam 13-Oct-17 8:06am    
read this
https://stackoverflow.com/a/4070437/1147428

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900