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

问题描述

今天我加入了这个网站,希望有人会好心地给我解释一下我与ASP.NET中的cookies做错了什么。
我还在学习,以便apoligies,如果我的问题是太基本的,但我找不到对谷歌的答案。每一个答案,我发现显示了code我已经有了。

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.

我创建和读取cookie的实验,我已经把这个code在我的应用程序的构造。这是我尝试初始化我的cookie并把它添加到浏览器中。

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

global.asax.cs

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

在HomeController.cs的方法(试图读取cookie)

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

我正在逐渐Response.Cookies.Add(myCookie时)的错误;
[HttpException(0X80004005):反应是在此上下文中可用​​的]

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?

推荐答案

可以;吨使用在Global.asax构造一个cookie添加到响应,因为应用程序开始处理HTTP请求之前在Global.asax构造函数被调用。

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

移动你的code在Global.asax构造的的Application_BeginRequest

Move your code in 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);
}

在Global.asax都有一个编号,火,你只是选择了错误不同的事件。

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


  • Application_Init :在应用初始化时,第一次火灾

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

  • 在session_start :火灾当用户的会话启动首次

  • 的Application_BeginRequest :每一个新的请求到来时火灾

  • Application_EndRequest :在请求结束时火灾

  • Application_AuthenticateRequest :表示一个请求已经被认证

  • 的Application_Error :当应用程序中发生未处理的错误火灾

  • Session_End中:每当一个用户会话结束或超时火灾

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

  • 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).

(从)

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

08-23 04:11