I joined this site today hoping someone would be kind enough to explain to me what i'm doing wrong with cookies in ASP.NET. I'm still learning so apoligies if my question is too basic, but i cannot find the answer on google. Every answer i find shows the code I already have.
I am experimenting with creating and reading cookies, i have put this code in my applications constructor. this is how i try to initialize my cookie and add it to the browser.
global.asax.cs
public MyApplication()
{
myCookie = new HttpCookie("UserSettings");
myCookie.Value = "nl";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);
}
a method in HomeController.cs (trying to read the cookie)
public void setLang(string lang)
{
HttpCookie myCookie = Request.Cookies["UserSettings"];
myCookie.Value = lang;
//rest of method
I am getting an error at Response.Cookies.Add(myCookie); [HttpException (0x80004005): Response is not available in this context.]
My thought is that i might have forgot to import a namespace or something, but nothing i do seems to fix this error, can anyone point me in the right direction?