I have a function that checks if a cookie (by name) exists or not:
Private Function cookieExists(ByVal cName As String) As Boolean
For Each c As HttpCookie In Response.Cookies
If c.Name = cName Then Return True
Next
Return False
End Function
I have a class that handles cookies in an application-specific manner, and I want to consolidate all the cookie-related functions to this class. However, I cannot use this code if I simply move it from the aspx page (where it currently resides) to the aforementioned class because I get the error: 'Name' Response is not declared.
I modified the class to allow the passing of a reference to the Response
object:
Public Function cookieExists(ByVal cName As String, ByRef Response As HttpResponse) As Boolean
For Each c As HttpCookie In Response.Cookies
If c.Name = cName Then Return True
Next
Return False
End Function
My question is: Is there a better way?
HttpContext.Current.Response
HttpContext.Current.Request
HttpContext.Current uses the Ambient Context design pattern, so you should be able to access the Response object from just about anywhere in your code. It is very useful.
For those wondering, the Ambient Context pattern is very cool, and is detailed here:
http://aabs.wordpress.com/2007/12/31/the-ambient-context-design-pattern-in-net/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With