代码之家  ›  专栏  ›  技术社区  ›  Richard Ev

httpContext.request.cookies和httpContext.response.cookies之间的关系

  •  5
  • Richard Ev  · 技术社区  · 14 年前

    我一直在尝试用代码清除 HttpContext.Response .

    最初,我用这个:

    DateTime cookieExpires = DateTime.Now.AddDays(-1);
    
    for (int i = 0; i < HttpContext.Request.Cookies.Count; i++)
    {
        HttpContext.Response.Cookies.Add(
            new HttpCookie(HttpContext.Request.Cookies[i].Name, null) { Expires = cookieExpires });
    }
    

    但是,如果 OutOfMemoryException 因为 for 循环从不退出-每次将cookie添加到 Response ,它也会添加到“请求”中。

    以下方法有效:

    DateTime cookieExpires = DateTime.Now.AddDays(-1);
    
    List<string> cookieNames = new List<string>();
    
    for (int i = 0; i < HttpContext.Request.Cookies.Count; i++)
    {
        cookieNames.Add(HttpContext.Request.Cookies[i].Name);
    }
    
    foreach (string cookieName in cookieNames)
    {
        HttpContext.Response.Cookies.Add(
           new HttpCookie(cookieName, null) { Expires = cookieExpires });
    }
    

    那么,到底是什么关系 HttpContext.Request.Cookies HttpContext.Response.Cookies ?

    1 回复  |  直到 14 年前
        1
  •  11
  •   SLaks    14 年前

    Request.Cookies

    Response.Cookies


    for