I am trying to create some cookies as I cannot fetch the cookies and a normal way so I will run through the whole header and just create the cookie manually. However whenever I try to use Response.Cookies and HttpCookie I get an error:
The name Response does not exist in the current content
and
The type or namespace name ‘HttpCookie’ could not be found (are you missing a using directive or an assembly reference?).
I read around and understood I will need to reference
System.Web
So I am clicking on Project -> Add Reference -> .NET and select System.Web. Then I add using System.Web;.
And still I am unable to access them.
From what code are you trying to access the item?
There are essentially two steps that need to be taken to reference a class:
usingdirective to the class’ namespace in the code file.It sounds like you’re performed the first step. What about the second? That’s what this error means:
To perform step 2 above, you actually have a couple of options:
HttpCookieyou would useSystem.Web.HttpCookie.usingdirective to the code file. At the top of the file (and this would need to be done for any file which uses classes in this namespace), you would addusing System.Web. Then you could just referenceHttpCookiedirectly.Additionally, the first part of your question brings up a separate issue entirely:
This again goes back to my question about where you’re accessing this value. When you’re writing code in, say, the code-behind for an ASPX page, you have access to the various bits of ASP.NET web contexts by default. These are provided by the base
Pageclass. Things likeRequestandResponseare readily available.However, if you’re trying to access these objects from outside of that context (such as from a custom class or another assembly), you’ll need to reference that context manually. This can be done by using a factory on the
System.Web.HttpContextclass:RequestbecomesSystem.Web.HttpContext.Current.RequestResponsebecomesSystem.Web.HttpContext.Current.ResponseThis is because
RequestandResponsearen’t actual classes. They’re properties on the current web context which are of typeHttpRequestandHttpResponse. Those properties are mapped for you when you’re in the same context, but outside of that context you’d need to reference them fully as demonstrated above.One thing to note is that it’s not generally considered good practice to have dependencies on the web context outside of where it’s already available. So if you’re creating a custom class or another assembly then you might want to take a step back and think about the design.
If your custom classes depend on the existence of a current instance of
HttpContextthen that code can never be used outside of a fully-aware web application context. So the code is less re-usable. So, depending on what you need from that context, you can re-factor the code to not need the context itself but only to require that the necessary items from the context be provided.This isn’t necessary to get you past your current issue, but it’s something to keep in mind going forward.