34

I have a ASP.NET with C# web application. One of the classes I created needs to use HttpContext.

According to http://msdn.microsoft.com/en-us/library/system.web.httpcontext(v=vs.90).aspx, HttpContext exists in the System.Web namespace in .NET 3.5 (which is the version I have installed).

However, when I write HttpContext. --> I don't see autocomplete. Which is what tells me that HttpContext is not recognized.

I did my homework and looked for usual solutions: 1. I added System.Web reference (by right clicking References -> choosing .NET tab and the particular reference). 2. I also made sure to include this line in the class: using System.Web;

Please tell me what else can I do. If all goes well, when I write HttpContext. - I am supposed to see a drop down list and be able to choose "Current" from there amongst several attributes/elements. I am new with C# and Visual Studio (2008) but I think Autocomplete not working well is a good indicator of a lacking reference/namespace/load errors/whatever else.

5
  • Just a quick question - where are you trying to use HttpContext? Is it Code behind? ASPX file? Another class? Commented May 15, 2012 at 7:02
  • Do you have the correct using? System.Web, I think? Commented May 15, 2012 at 7:04
  • Andrew: I am using System.Web. Katalonis, I am trying to use HttpContext inside a CLASS (a .CS file added into the App_Code folder). [...] using System.Web; public static class Utilities { static Utilities() { } public static void LogError(Exception ex) { System.Web.HttpContext context = System.Web.HttpContext.Current; ..... This is how the class starts, if it helps! Thank you! Commented May 15, 2012 at 7:37
  • 1
    Just a guess, but maybe the class is not set to compile? Click on the .CS file in a solution explorer, go to properties, and Build action should be set to compile. Commented May 15, 2012 at 7:45
  • Another related question: is it recommendable or useful to have all .cs files with Build action = Compile, by DEFAULT? Is there a way to do that? I stumbled upon this property just yesterday and did use it for a few classes. ******You were all FANTASTIC and very prompt, special thanks to KATALONIC: indeed the class was not set to compile! I forgot that classes are set as "Content" (not Compile) by default! I like this place! Hope to contribute similarly in the future. Commented May 15, 2012 at 8:03

3 Answers 3

43

The constructor is rarely used when you want to instantiate the HttpContext class. I always use the static property HttpContext.Current which is the current instance used by all the ASP.Net application.

For using it make sure you already add reference to the System.Web.dll assembly and import the System.Web namespace

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

4 Comments

I'm using vs 2015 with dotNet 4.5 and this does NOT work. The System.Web doesn't have any HttpContext class.
... unless you install NuGet package "Microsoft.AspNet.WebApi"
I have the same issue, this doesnt work for me either, i'm able to import System.web, but it simply cannot find the httpcontext
Thank you. I had to bring my script to my laptop to do some code updates, and it was giving an error on the "HttpContext.Context", which I did not remember putting in, as the others were "HttpContext.Current". Anyway, thank you.
6

It was a simple case of not using the right framework, by that I mean the full fat version rather than the default 'light' version.

Right click on the Project and then Properties and make sure the full version of the latest framework is selected ie '.NET Framework 4', not '.NET Framework 4 Client Profile'

1 Comment

TIP: for vs 2015, make sure you install NuGet package "Microsoft.AspNet.WebApi" in your project. Installing the "Microsoft.AspNet.WebApi.Core" is NOT enough.
4
  1. Make sure the assembly is included
  2. Make sure the reference for the dll is there
  3. Try "resolve" and have the Visual Studio include the reference
  4. Debug, close and restart the application.

This class should work.

UPDATE:

using System.Web

class YourClass {
  public YourClass() {
    HttpContext _context = new HttpContext(your parameters)
  }
}

4 Comments

Hello jefpw, thank you for answering! Could you please elaborate a bit on your answer? As I said, I am new. 1. How do I include the assembly, maybe I've already done it, not sure what you mean? 2. As for including the reference, I did that as described above (by right clicking References -> choosing .NET tab and the particular System.Web reference). 3. What do you mean by trying "resolve"? Thanks a lot!
See above. HttpContext is pretty advanced class if you are a beginner. Why do you need HttpContext. I haven't used HttpContext in ASP.NET and I have used ASP.NET for 4-5 years.
iefpw: What I need HttpContext for - from what I understand, HttpContext.Current.Request.RawUrl displays the current page URL (short form of it) and I will display that information on a specially customised error page. This is only my second website. Using all the resources, tricks and advice I can find online. Wow, 4 years working in the field. Congratulations!
Anna, use the Context property of the webpage. You don't need to create new HttpContext. Context will capture the current default HttpContext.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.