Strange behavior with HttpCookieCollection

Discussion in 'ASP .Net' started by adlaird@earthlink.net, Jul 19, 2003.

  1. Guest

    Hi,

    I created a simple webform, put a test button on it and implemented a
    click handler that looks like so:

    private void btnTest_Click(object sender, System.EventArgs e)
    {
    HttpCookieCollection oCookReq =
    HttpContext.Current.Request.Cookies;
    HttpCookieCollection oCookResp =
    HttpContext.Current.Response.Cookies;

    int n1 = oCookReq.Count;
    int n2 = oCookResp.Count;
    bool bIsNull = (oCookResp["someCookieName"] == null);
    int n3 = oCookReq.Count;
    int n4 = oCookResp.Count;

    Response.Write(string.Format("Before: Request = {0}, Response
    = {1}<br>", n1, n2));
    Response.Write(string.Format("After: Request = {0}, Response
    = {1}<br>", n3, n4));
    }


    When I click the button the test output on the page is:

    Before: Request = 1, Response = 0
    After: Request = 2, Response = 1

    Can anyone explain why the mere act of looking for a cookie in the
    response collection would cause one to get added to both the response
    and request collections? I tried the test again using a new
    HttpCookieCollection (i.e. not one from the current context) and do
    not get this behavior. Clues?

    Thanks much...

    - A
     
    , Jul 19, 2003
    #1
    1. Advertising

  2. wrote in
    news::

    > Hi,
    >
    > I created a simple webform, put a test button on it and
    > implemented a click handler that looks like so:
    >
    > private void btnTest_Click(object sender, System.EventArgs e)
    > {
    > HttpCookieCollection oCookReq =
    > HttpContext.Current.Request.Cookies;
    > HttpCookieCollection oCookResp =
    > HttpContext.Current.Response.Cookies;
    >
    > int n1 = oCookReq.Count;
    > int n2 = oCookResp.Count;
    > bool bIsNull = (oCookResp["someCookieName"] == null);
    > int n3 = oCookReq.Count;
    > int n4 = oCookResp.Count;
    >
    > Response.Write(string.Format("Before: Request = {0},
    > Response
    >= {1}<br>", n1, n2));
    > Response.Write(string.Format("After: Request = {0},
    > Response
    >= {1}<br>", n3, n4));
    > }
    >
    >
    > When I click the button the test output on the page is:
    >
    > Before: Request = 1, Response = 0
    > After: Request = 2, Response = 1
    >
    > Can anyone explain why the mere act of looking for a cookie in
    > the response collection would cause one to get added to both the
    > response and request collections? I tried the test again using
    > a new HttpCookieCollection (i.e. not one from the current
    > context) and do not get this behavior. Clues?


    You've stumbled upon a frustrating (and, AFAIK, undocumented) aspect
    of the ASP.NET cookies response collection. Simply querying the
    collection will create the cookie if it doesn't exist.

    Here's an article that explains this in more depth:

    http://www.codeproject.com/aspnet/AspNetCookies.asp

    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
     
    Chris R. Timmons, Jul 19, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sstark
    Replies:
    0
    Views:
    463
    sstark
    Mar 6, 2005
  2. ryang
    Replies:
    1
    Views:
    952
    Wes Groleau
    Apr 11, 2005
  3. Apogee

    Strange Behavior with ViewState

    Apogee, Jul 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    327
    Apogee
    Jul 3, 2003
  4. PJ

    DropDownList Strange Behavior

    PJ, Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    354
  5. Mantorok Redgormor
    Replies:
    70
    Views:
    1,773
    Dan Pop
    Feb 17, 2004
Loading...

Share This Page