代码之家  ›  专栏  ›  技术社区  ›  Random Developer

应用程序日志中的global.asax警告中的cookie加载项

  •  0
  • Random Developer  · 技术社区  · 16 年前

    在global.asax文件中,我有以下内容:

    void Session_End(object sender, EventArgs e) 
    {
        System.Web.HttpCookie isAccess = new System.Web.HttpCookie("IsAccess");
        isAccess.Expires = DateTime.Now.AddDays(-1);
        isAccess.Value = "";
        System.Web.HttpContext.Current.Response.Cookies.Add(isAccess);
    }
    

    因此,每次在应用程序中调用此方法时,应用程序日志中都会记录以下事件作为警告:

    Event code: 3005 
    Event message: An unhandled exception has occurred. 
    Event time: 5/25/2010 12:23:20 PM 
    Event time (UTC): 5/25/2010 4:23:20 PM 
    Event ID: c515e27a28474eab8d99720c3f5a8e90 
    Event sequence: 4148 
    Event occurrence: 332 
    Event detail code: 0 
    
    Application information: 
        Application domain: /LM/W3SVC/2100509645/Root-1-129192259222289896 
        Trust level: Full 
        Application Virtual Path: / 
        Application Path: <PathRemoved>\www\ 
        Machine name: TIPPER 
    
    Process information: 
        Process ID: 6936 
        Process name: w3wp.exe 
        Account name: NT AUTHORITY\NETWORK SERVICE 
    
    Exception information: 
        Exception type: NullReferenceException 
        Exception message: Object reference not set to an instance of an object. 
    
    Request information: 
        Request URL:  
        Request path:  
        User host address:  
        User:  
        Is authenticated: False 
        Authentication Type:  
        Thread account name: NT AUTHORITY\NETWORK SERVICE 
    
    Thread information: 
        Thread ID: 7 
        Thread account name: NT AUTHORITY\NETWORK SERVICE 
        Is impersonating: False 
        Stack trace:    at ASP.global_asax.Session_End(Object sender, EventArgs e) in <PathRemoved>\Global.asax:line 113
    

    知道这个代码为什么会导致这个错误吗?

    3 回复  |  直到 16 年前
        1
  •  2
  •   C. Dragon 76    16 年前

    在会话结束事件期间,您似乎正在尝试修改cookie。这不起作用,因为会话结束(会话结束)与用户的HTTP请求不对应。相反,它是由ASP.NET在从特定用户收到最后一个HTTP请求后20分钟(或会话超时设置为的任何时间)启动的。

        2
  •  0
  •   Sky Sanders    16 年前

    知道这个代码为什么会导致 这个错误?

    我的 猜测 是指您在不存在响应的方法中访问响应。

    您使用哪种global.asax方法调用此代码?

        3
  •  0
  •   joelt    16 年前

    看起来会话结束了……可能没有httpContext,更不用说响应了。

    如果你只是想在会话结束时清除一个cookie,难道你不能不设置一个过期日期,当用户关闭浏览器时让它清除吗?