Server.MapPath()
Asked Answered
P

3

5

I want to use Server.MapPath() method im order to map a virtual directory I created to its physical path.

The thing is that the .net environment doesn't recognize Server.MapPath().

Google told me I'm supposed to use HttpContext.Current.Server using System.Web, but HttpContext isn't recognized in spite of me using System.Web. (And I've checked - HttpContext IS one of System.Web's classes)

Help?

Pennate answered 5/10, 2009 at 9:11 Comment(4)
'.net environment doesn't recognize Server.MapPath()'. Is there any error?Loriloria
Have you included System.Web as a reference of the project?Gerardogeratology
Can you share a code sample of what you are trying to do?Scampi
It was really silly of me to ask this question without providing you at least the version of .NET I'm using. Sorry about that. I've solved the proble. The thing was I'm using .NET 3.5 which no longer includes server.mappath() but rather HttpServerUtility.MapPath() 10x! :)Pennate
E
3

Make sure you have included System.Web in your projects References Do these (In Visual Studio IDE):

  1. Right click on the Project Node (Solution Explorer Window)
  2. On the context mennu, click Add Reference
  3. Select System.Web on the .NET Tab list items.
  4. Hit OK button

Server.MapPath should now be available.

Eringo answered 5/10, 2009 at 9:20 Comment(1)
This solution worked for me as I was working in a class library.Enisle
B
3

If you have a web application, you should automatically have a reference to System.Web.dll, and you should have access to the System.Web.HttpContext class. Check that you haven't accidentally removed the reference. You would need a using System.Web; statement to access the HttpContext class without specifying the complete namespace.

If you don't have a web application you would have to add a referece to System.Web.dll to get access to the HttpContext class, but that would not help you a bit. As you are not in a web application, there is no HTTP context and there is no web root folder, so you can not use the MapPath method.

Barnum answered 5/10, 2009 at 9:22 Comment(2)
It might be a method in a separate assembly that is used by the web app, so the context might be there after all.Gerardogeratology
@rslite: Yes, you are right. That's also a possible reson why the reference might be missing.Barnum
M
1

Same problem here. In an ASP.net 4.0 web application, in a .ashx handler, with a using System.Web at the top. I couldn't use Server.MapPath() which is what the book I have says to use or System.Web.HttpServerUtility.MapPath() which is what Google and MSDN keep turning up. I also couldn't use HttpServerUtility.MapPath() as mentioned above.

However, one of the other answers here prompted me to try context.Server.MapPath() which does work in my ProcessRequest(HttpContext context) method.

Madelyn answered 12/6, 2011 at 19:20 Comment(1)
To be even more precise: HttpContext.Current.Server.MapPath()Incrocci

© 2022 - 2024 — McMap. All rights reserved.