问题
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.
回答1:
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
回答2:
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'
回答3:
- Make sure the assembly is included
- Make sure the reference for the dll is there
- Try "resolve" and have the Visual Studio include the reference
- Debug, close and restart the application.
This class should work.
UPDATE:
using System.Web
class YourClass {
public YourClass() {
HttpContext _context = new HttpContext(your parameters)
}
}
来源:https://stackoverflow.com/questions/10595543/system-web-httpcontext-not-recognized