Visual Studio 2005 – Application_BeginRequest – Save Cookies

I have worked with cookies so many times but first time i am writing in my blog.

I received an email from one of my sender that has issue with cookies.

He wrote:

I’m attempting to save a cookie which tracks the last page the user
visited whilst on my aspx 1.1 application.  It seems logical to place
this code into the global.asax file within Application_BeginRequest;
such as
protected void Application_BeginRequest(Object sender, EventArgs e)
{
       HttpCookie test = new HttpCookie(“test”);
       test.Values[“Index”] = “POP”;
       HttpContext.Current.Request.Cookies.Add(test);
}

however this never creates the cookie… does anyone know why? 

Reply:

Global.asax.csprotected void Application_BeginRequest(Object sender, EventArgs e)

{

HttpCookie test = new HttpCookie(“MyCookie”,”Hi Buddy”);

Response.Cookies.Add(test);

}

Page.aspx.cs

private void Page_Load(object sender, System.EventArgs e)

{

Response.Write(Request.Cookies[“MyCookie”].Value);

}

Hope this will solve a problem of so many peoples.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s