线程调用方法 System.Web.HttpContext.Current.Server.MapPath()

来源:互联网 发布:unity3d 多人在线 编辑:程序博客网 时间:2024/05/18 16:13
在多线程里面使用HttpContext.Current,HttpContext.Current是得到null的. 
所以在线程调用方法,方法中类里面的System.Web.HttpContext.Current.Server.MapPath() 获取不到对象。

应该这么用: 

public static string MapPath(string strPath) { if (HttpContext.Current != null) { return HttpContext.Current.Server.MapPath(strPath); } 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); } }


原创粉丝点击