10. January 2009 13:49
The Server.MapPath function a very useful, but it requires an HTTP Context or a Null exception error is thrown.
In situations where you don't have an HTTP context like a property in a class file, a slightly different approach is required. I had to do some digging but I found this and it worked perfectly for me. Let's say you have a String property on a class and you want a separate READONLY property to represent the full Server.MapPath equivalent. Instead of using Server.MapPath("~/"+Me.OutFile), just use this:
System.Web.Hosting.HostingEnvironment.MapPath("~\" + Me.OutFile)
8. December 2008 08:48
Learning is the name of the game with ASP.Net. Today I was trying to use Server.MapPath in my class library. But it always throw an error and Visual Studio wouldn't suggest which Import statement to use.
Finally figured out you have specifically add a reference to System.Web in your project. Apparently "Imports System.Web" at the top of your class file isn't enough.
So, once you add a reference to System.Web, you can now use System.Web.HttpContext.Current.Server.MapPath()