none
Item has already been added. Key in dictionary: '.XXX_YYY' Key being added: '.XXX_YYY' RRS feed

  • Question

  • Hi there,

    I would like to ask you a question.

    According to this documentation https://msdn.microsoft.com/en-us/library/system.web.httpresponse.setcookie(v=vs.110).<g class="gr_ gr_35 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="35" id="35">aspx</g>, you suggested that HttpResponse.SetCookie Method (HttpCookie) is not intended to be used directly from your code.
    Could you please explain why? Because we're experiencing a problem with cookie name while we use some code as below:

    var cookieBrowserID = Context.Request.Cookies[CookieName_BrowserID] ?? new HttpCookie(CookieName_BrowserID);
                var cookieVI = Context.Request.Cookies[CookieName_VisitorIdentifier] ?? new HttpCookie(CookieName_VisitorIdentifier);
    .......
    Context.Response.SetCookie(cookieBrowserID);
    Context.Response.SetCookie(cookieVI);

    This is the stack trace:
    System.ArgumentException: Item has already been added. Key in dictionary: '.XXX_YYY' Key being added: '.XXX_YYY'
    at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
    at System.Collections.Specialized.NameObjectCollectionBase.BaseAdd(String name, Object value)
    at System.Web.HttpRequest.FillInCookiesCollection(HttpCookieCollection cookieCollection, Boolean includeResponse)
    at System.Web.HttpRequest.ResetCookies()

    Could you please give some advice? I really appreciate your help.

    Friday, June 16, 2017 10:20 AM

All replies

  • Hi Gaubeou,

    Thank you for posting here.

    For your question, could you provide more information? 

    I test your code and there is no error for me. HttpResponse.SetCookie method is to update the cookie. When I use add method to add cookie and then use SetCookie method. The value of cookie would be changed. And there is only one XXX_YYY key.

    Could you provide the more information about the error message and code sample for us to reproduce the error?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 19, 2017 9:20 AM
    Moderator