Alternative to Server.MapPath…use HostingEnvironment.MapPath

A call to the following context sensitive MapPath function does exactly what the documentation says it does.

System.Web.HttpContext.Current.Server.MapPath(“\filename.txt”);

It returns the physical file path that corresponds to the specified virtual path on the Web server. But this isn’t always what you want as it works on the current context which may be a web form running in a sub-directory of the main website.

In this case the solution is simple. Use the following code to map the path to a physical path on the server

System.Web.Hosting.HostingEnvironment.MapPath(“\filename.txt”);

Tagged , . Bookmark the permalink.

Leave a Reply