EDIT:
I just found that it's possible to use HostingEnvironment.MapPath()
instead of HttpContext.Current.Server.MapPath()
I haven't tried it yet in a thread or timer event though.
ORIGINAL ANSWER:
I think the answer to my question is "You just can't..."
Some solutions I thought about :
The only method I care about on HttpServerUtility
is MapPath
. So as an alternative I could use AppDomain.CurrentDomain.BaseDirectory
and build my paths from this. But this will fail if your app uses virtual directories (Mine does).
Another approach :
Add all the paths I need to the the Global
class. Resolve these paths in Application_Start.
Code:
public class Global : System.Web.HttpApplication
{
private static string _appRootDir;
public static string AppRootDir
{
get
{
return _appRootDir;
}
}
private static string _usersFilesDir;
public static string UsersFilesDir
{
get
{
return _usersFilesDir;
}
}
private static string _tempFilesDir;
public static string TempFilesDir
{
get
{
return _tempFilesDir;
}
}
protected void Application_Start(object sender, EventArgs e)
{
_appRootDir = HttpContext.Current.Server.MapPath("/");
_usersFilesDir= HttpContext.Current.Server.MapPath("/UsersFiles");
_tempFilesDir= HttpContext.Current.Server.MapPath("/TempFiles");
}
}