none
Asp .net HttpCookieCollection.Get bug report. RRS feed

  • Question

  • This page sais HttpCookieCollection.Get function could get the HttpCookie instance with the specific name, if the cookie instance does not exists in the browser, this function will create a new instance with that name.

    But in my experience, this function will return null and doesn't create a new one if the specific cookie doesn't exists. Please refer to the pic.

    And when I try to get the value, the System.NullReferenceException occurs.

    The version of my .Net framework is 4.6 with Asp .Net CLR 4.0

    I think either the .Net framework or the documentation made some mistake about HttpCookieColleation method.


    Friday, September 8, 2017 3:09 AM

Answers

  • That would break common usage pattern that people directly chain the calls like this (although they should have changed it when they test the code and see it breaks):

    string SomeValue = Request.Cookies.Get("SomeValue").Value;

    string SomeValue2 = Request.Cookies["SomeValue2"].Value;

    I checked the source and see they do create a new instance of cookie if they don't find it, provided that Response is not null.

    That's strange to me.

    P.S.: I've filed a request to ask them change the Remarks section to reflect this condition. See when the change will be made to the documentation.


    Friday, September 8, 2017 6:43 AM
    Answerer

All replies

  • Most likely implementation bug. AFAIK the documented behavior stays the same since .NET v1.X in order to maintain compatibility with old ASP code.

    Please file a bug report with feedback tool located at one of the buttons on the upper right corner of IDE to make sure they see it.



    Friday, September 8, 2017 4:08 AM
    Answerer
  • Probably creating the cookies only works (and has sense) in case of Response.Cookies.Get.

    Friday, September 8, 2017 4:54 AM
  • That would break common usage pattern that people directly chain the calls like this (although they should have changed it when they test the code and see it breaks):

    string SomeValue = Request.Cookies.Get("SomeValue").Value;

    string SomeValue2 = Request.Cookies["SomeValue2"].Value;

    I checked the source and see they do create a new instance of cookie if they don't find it, provided that Response is not null.

    That's strange to me.

    P.S.: I've filed a request to ask them change the Remarks section to reflect this condition. See when the change will be made to the documentation.


    Friday, September 8, 2017 6:43 AM
    Answerer
  • Thanks Cheong, that is also very strange to me, I have also report this issue in VS feedback center, and discuss about solve this issue. I'll keep following about the solution.

    BR.

    Friday, September 8, 2017 10:04 AM