c# - Difference between HttpResponse: SetCookie, AppendCookie, Cookies.Add -


there different ways create multi value cookies in asp.net:

var cookie = new httpcookie("mycookie"); cookie["information 1"] = "value 1"; cookie["information 2"] = "value 2";  // first way response.cookies.add(cookie);  // second way response.appendcookie(cookie);  // third way response.setcookie(cookie); 

when should use way? i've read setcookie method updates cookie, if exits. doesn't other ways update existing cookie well?

and following code best practice writing single value cookies?

response.cookies["mycookie"].value = "value";

when should use way?

it's depends on cookie operation want do.

note add , appendcookie doing same functionality except fact appendcookie you're not referencing cookies property of response class , it's doing you.

  • response.cookies.add - adds specified cookie cookie collection.
  • response.appendcookie - adds http cookie intrinsic cookie collection
  • response.setcookie - updates existing cookie in cookie collection.

exceptions not thrown when duplicates cookies added or when attempting update not-exist cookie.

the main exception of these methods is: httpexception (a cookie appended after http headers have been sent.)

the add method allows duplicate cookies in cookie collection. use set method ensure uniqueness of cookies in cookie collection.

thanks msdn!


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -