读/写Cookies [英] Reading/Writing Cookies

查看:219
本文介绍了读/写Cookies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天加入了这个网站,希望有人能够向我解释我在ASP.NET中做什么错误。
如果我的问题太基础,我仍然在学习如此的apoligies,但我不能在google上找到答案。每一个答案,我发现显示我已经有代码。



我正在试验创建和读取cookie,我把这个代码在我的应用程序构造函数。这是我尝试初始化我的cookie并将其添加到浏览器。



global.asax.cs

  public MyApplication()
{
myCookie = new HttpCookie(UserSettings);
myCookie.Value =nl;
myCookie.Inpires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);
}

HomeController.cs中的一个方法>

  public void setLang(string lang)
{
HttpCookie myCookie = Request.Cookies [UserSettings];
myCookie.Value = lang;
//方法的剩余

我收到一个错误Response.Cookies.Add myCookie);
[HttpException(0x80004005):响应在此上下文中不可用。]



我的想法是我可能忘了导入命名空间没有什么我似乎解决这个错误,任何人可以指出我的方向正确?

解决方案

.asax构造函数向响应中添加一个cookie,因为在应用程序开始处理HTTP请求之前调用了Global.asax构造函数。



将您的代码从Global.asax构造函数移动到 Application_BeginRequest 方法:

  public void Application_BeginRequest()
{
myCookie = new HttpCookie(UserSettings);
myCookie.Value =nl;
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);
}

Global.asax有许多不同的事件被触发,




  • Application_Init :当应用程序第一次初始化时触发。

  • Application_Start :应用程序第一次启动时触发。

  • Session_Start :在用户会话开始时首次启动。

  • Application_BeginRequest :每次

  • code> Application_AuthenticateRequest :表示请求已准备好通过身份验证。

  • Application_Error

  • Session_End :当单个用户会话结束或超时时触发。

  • Application_End :应用程序结束或超时时触发(通常用于应用程序清理逻辑)。



    • (来自 http://en.wikipedia。 org / wiki / Global.asax


      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?

      解决方案

      You can't use the Global.asax constructor to add a cookie to the Response because the Global.asax constructor is called before the application starts processing the HTTP request.

      Move your code from the Global.asax constructor to the Application_BeginRequest method:

      public void Application_BeginRequest()
      {
          myCookie = new HttpCookie("UserSettings");
          myCookie.Value = "nl";
          myCookie.Expires = DateTime.Now.AddDays(1d);
          Response.Cookies.Add(myCookie);
      }
      

      The Global.asax has a number of different events that are fired, you just chose wrongly.

      • Application_Init: Fires when the application initializes for the first time.
      • Application_Start: Fires the first time an application starts.
      • Session_Start: Fires the first time when a user’s session is started.
      • Application_BeginRequest: Fires each time a new request comes in.
      • Application_EndRequest: Fires when the request ends.
      • Application_AuthenticateRequest: Indicates that a request is ready to be authenticated.
      • Application_Error: Fires when an unhandled error occurs within the application.
      • Session_End: Fires whenever a single user Session ends or times out.
      • Application_End: Fires when the application ends or times out (Typically used for application cleanup logic).

      (from http://en.wikipedia.org/wiki/Global.asax)

      这篇关于读/写Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆