1

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.

2
  • After you add the reference, what errors are you getting? Commented Apr 3, 2012 at 18:47
  • I am getting the exact same error, as if i did not add the reference Commented Apr 3, 2012 at 19:00

1 Answer 1

2

From what code are you trying to access the item?

There are essentially two steps that need to be taken to reference a class:

  1. Add a reference to the class' assembly in the project.
  2. Add a using directive 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:

The type or namespace name 'HttpCookie' could not be found (are you missing a using directive or an assembly reference?).

To perform step 2 above, you actually have a couple of options:

  1. Fully-qualify the class name. So, instead of using HttpCookie you would use System.Web.HttpCookie.
  2. Add a using directive 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 add using System.Web. Then you could just reference HttpCookie directly.

Additionally, the first part of your question brings up a separate issue entirely:

The name Response does not exist in the current context

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 Page class. Things like Request and Response are 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.HttpContext class:

  1. Request becomes System.Web.HttpContext.Current.Request
  2. Response becomes System.Web.HttpContext.Current.Response

This is because Request and Response aren't actual classes. They're properties on the current web context which are of type HttpRequest and HttpResponse. 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 HttpContext then 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.

Sign up to request clarification or add additional context in comments.

10 Comments

So as you probably understood i am a novice Visual studio user, i basically learn as i try stuff, my current project is to log to a website in a click of a button. so i have the code to do it in the button. About HttpCookie its seem i've already fixed it with the reference and using system.web - its seems to be working, though about request / reqsones they both arent working unless i use your "System.Web.HttpContext.Current.Request" and "System.Web.HttpContext.Current.Respones" so the problem seem to be solved however i dont know how i am accessing them
@YanivKossas: That's fine. New developers are certainly welcome at Stack Overflow. We value one's ability to ask a coherent question above one's ability to find the solution. While there's certainly a language barrier, I think your question was fairly clear. The main issue you were having was that you weren't interpreting the error messages correctly, and that comes only from experience.
Basically i just opened a window document and started working from there so i am not sure from where i am accessing it. I will try and use the above help you gave me and if i will stumble upon a new problem i will search for a solution then.
@YanivKossas: I'm not sure what you mean by "a window document." Are you working inside of Visual Studio? Generally the process for creating something new in Visual Studio is File->New->Project where you choose the project type and where to save it. Then you'd primarily add new things from the Solution Explorer window. For example, to add a new class to the project, you'd right click on the project name or the folder within the project where you want to add it and select Add->New Item and choose the item type. To get you started, you might want to look for tutorials or books.
That is exactly what i did, file -> new -> project and window form application. well from my previous experience with C and php reading from books is nice but not a good learning way, seem i get things better if i try them.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.