在多线程里面使用HttpContext.Current,HttpContext.Current是得到null的获取路径方法

原因:20150703 解释下为什么当前请求上下文会为null,因为多线程情况下,当前线程可能并非http请求处理线程,根本没发生请求,所以无法获取到HttpContext就是null.
public static string MapPath(string strPath)
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);//有http请求
}
else //非web程序引用
{
strPath = strPath.Replace("/", “\”);
if (strPath.StartsWith("\"))
{
//strPath = strPath.Substring(strPath.IndexOf(’\’, 1)).TrimStart(’\’); strPath = strPath.TrimStart(’\’);
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}

发布了55 篇原创文章 · 获赞 2 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/wqs15192095633/article/details/90489735