Search code examples
asp.netcookieshttpcontext

using HttpContext.Current.Request.Cookies.Set()


I've seen this in some code and I'm a little confused about when you would use that method. Sure, the Get() method makes sense for a request, but wouldn't you ordinarily use a Set() method for cookies in the Response.Cookies object, rather than Request.Cookies?


Solution

  • Let's say you have some code that expects a cookie in the request, but under some circumstances this cookie is not present. In this case you could set the cookie in the request to a default value.

    Also, if you are testing code that relies on cookies being present in the request (when doing unit testing for example), then of course you need a way to set cookies without a browser (unit testing calls for testing code in isolation).