多线程下HttpContext.Current 值为null
在网上看见
public static string MapPath(string strPath)
{
if (System.Web.HttpContext.Current != null)
{
return System.Web.HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "");
strPath = strPath.Replace("~","");
if (strPath.StartsWith("//"))
{
strPath = strPath.TrimStart('//');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}
这样可以解决,但是我想知道出错的原因 以及这样做的原理 有大神吗?