.net, do I need to send to the browser?

After creating or modifying the httpcookie, do I need to add it to the response.cookies collection? (if yes, only if its a new cookie or even if I change it)

+1


a source to share


2 answers


If it's new, it needs to be added to the Response.Cookies collection to be saved, but you can access the collection as an array.

Response.Cookies["foo"] = myCookie;

      

This will take care of the addition for you. FYI, if no cookies were created, the value will return null.



However, the cookie will be updated if you reference the collection like this:

HttpCookie myCookie = Response.Cookies["foo"];

      

Any changes to myCookie will be made to the cookie in the collection, because in C #, when you assign a variable to an existing class object, that object is referenced and modified.

+1


a source


If you don't add a cookie to the Response.Cookies collection, it will never be sent to the browser. However, once you add the cookie to the collection, you can change it however you like, and the last value you set is what will be sent to the browser. What you add to the collection is a link to your HttpCookie object and the cookie is not sent to the browser until the end of the request's lifecycle.



+1


a source







All Articles