Perform Server.MapPath When HttpContext Not Available

By Peter Bromberg

There are certain situations - particularly in Global.asax where you need to map a path but the HttpContext.Current property is null. Here is a simple method that works around this issue.

public static string MapPath(string path)
{
if (HttpContext.Current != null)
return HttpContext.Current.Server.MapPath(path);

return HttpRuntime.AppDomainAppPath + path.Replace("~", string.Empty).Replace('/', '\\');
}

Perform Server.MapPath When HttpContext Not Available  (1669 Views)