views:

83

answers:

4

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?

+3  A: 

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.

Timwi
You rock! Thanks for the screen cast ;)I thought typing "Using System.Web" is enough. Maybe I have to go though this menu to set using directives. OR the IDE did not recognize the copied code. Magic somehow, because nothing changed in my file ;)
OneWorld
@user: If Timwi helped you out, be sure to mark this as an answer. Do note that assembly references and using statements are different animals. Assembly references determine what code is available for your application to consume. Namespace using statements provide only convenient shorthand for referring to classes without their full namespace-qualified name. You'll find that adding references will change only your csproj/vbproj file.
kbrimington
@user433718: In addition to what @kbrimington said above, also let me point out that the assembly’s name is not always the same as the namespace name (which makes it very confusing). For example, a lot of the WPF stuff is in the assembly *PresentationCore* but in the namespace *System.Windows.Media*.
Timwi
@kbrimington: I didn't knew that before I reconized the OK-Button now. I wanted to rate it up, but that was disabled for me. So, Im gonna close this question now as answered.Thanks for clearifying the issue in the last two comments!
OneWorld
A: 

Try to add a reference to System.Web in your project.

HttpContext is a member of System.Web.

http://msdn.microsoft.com/en-us/library/system.web.httpcontext.aspx

Pierre 303
I already had "Using System.Web"But when I try to type "Using System.Web.HttpContext" it says "HttpContext" not found in the namespace "System.Web"I also tried "System.Web.HttpContext.Current.Server.MapPath()" in the code. At this point Visual Studio said again "not in the namespace..."
OneWorld
@user433718: You can't put `HttpContext` in your `using` statement, as it's a class, not a namespace.
Guffa
The IDE can't automatically detect the namespace if the DLL is not referenced. If you want to be able to do that even with not referenced DLL's, I don't think it's possible.
Pierre 303
A: 

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.

Guffa
Thanks for this comment. I will look into that. I didnt know that before. Just wanted to get it working somehow...
OneWorld
Yep, I even dont need this Code! *lol* Thank you! I'm sure this would have been my next question ;)
OneWorld
A: 

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.

David Culp