Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find HttpContext in .NET 3.5 and Visual Studio 2008

I used code from here and I get the following error: Can't use HttpContext.Current.Server.MapPath()

In Visual Studio 2008 does the ContextMenuEntry "Solve" help you when you have missing references?

I already found out that HttpContext is not a member of System.Web in my IDE. According to Help > Info I am using .NET 3.5 SP1.

How do I get that running?

How do u usually react in this situation? What stuff do u look for in msdn.com?

like image 944
OneWorld Avatar asked Aug 28 '10 13:08

OneWorld


People also ask

Where is HttpContext from?

When an HTTP request arrives at the server, the server processes the request and builds an HttpContext object. This object represents the request which your application code can use to create the response.

What's the HttpContext object?

The object provides access to the intrinsic Request, Response, and Server properties for the request. This object is ready for garbage collection when the HttpRequest is completed.

Is HttpContext current thread safe?

HttpContext is not thread safe! No deadlocks if you block a Task with Task. Wait or Task.


3 Answers

What I would do in that situation is look on MSDN (or Google) for HttpContext. I did that, and it says it’s in System.Web. So make sure your project has a reference to System.Web.

“Add Reference” menu item

“System.Web” in the Add Reference dialog

... and then it seems to work:

HttpContext is now available.

like image 124
Timwi Avatar answered Oct 04 '22 01:10

Timwi


You can look in the documentation for the HttpContext class, and it tells you that it's in the System.Web namespace, in the System.Web.dll library.

So, to use it you need a reference to the System.Web.dll library, and you either need a using System.Web; statement, or use the fullly qualified name System.Web.HttpContext.Current.Server.MapPath.

However, are you sure that you want to use the MapPath method? The method gets the physical path of a web reference to a file. If the path to your CSV file is a web reference, for example "/data/items.csv" then you want to use the MapPath method, but if you have a physical path like for example "C:\mydata\items.csv" then you don't want to convert it.

Also, the MapPath only works if you actually are in a web application, where there is a HTTP context.

like image 34
Guffa Avatar answered Oct 04 '22 01:10

Guffa


Timwi has it right, but for completeness. No, VS does not have the 'Solve' capability built in, however this functionality has been partially added by some add-ons. For example, Resharper will add the option to add the reference and using when needed -- but it does have to have been referenced before in the solution so it doesn't solve the initial find problem.

like image 34
David Culp Avatar answered Oct 04 '22 00:10

David Culp