社区
C#
帖子详情
线程Thread调用时System.Web.HttpContext.Current为NULL的问题
yellowtree12
2011-11-04 05:16:06
我想调用System.Web.HttpContext.Current.Server.MapPath("strMath"),可是提示错误,系统找不到该文件,如果我的文件是放在C:\windosw下,那这段代码该怎样写System.Web.HttpContext.Current.Server.MapPath("strMath"),注意是线程Thread调用。
...全文
312
6
打赏
收藏
线程Thread调用时System.Web.HttpContext.Current为NULL的问题
我想调用System.Web.HttpContext.Current.Server.MapPath("strMath"),可是提示错误,系统找不到该文件,如果我的文件是放在C:\windosw下,那这段代码该怎样写System.Web.HttpContext.Current.Server.MapPath("strMath"),注意是线程Thread调用。
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
6 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
xysxjf
2011-12-02
打赏
举报
回复
自定义一个方法
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);
}
}
wsxqaz
2011-11-04
打赏
举报
回复
Environment.CurrentDirectory
krenyelang
2011-11-04
打赏
举报
回复
string strpath=@"C:\windosw";
yellowtree12
2011-11-04
打赏
举报
回复
我是在cs里面写这个,怎样在cs里面使用这个获取应用程序根目录的路径?
leoboss1
2011-11-04
打赏
举报
回复
我也不会
wsxqaz
2011-11-04
打赏
举报
回复
你是在asp.net程序里调用了这个么?
线程
Thread
调用
时
System.
Web
.
Http
Context
.
Current
为
NULL
在采用
线程
异步处理
时
,
线程
调用
的方法中如果引用System.
Web
,并使用其
Http
Context
.
Current
.Server.MapPath()方法
时
,其实返回的是
NULL
值,其结果就是产生“未将对象引用到对象实例”的异常。 解决方法:自定义一个静态方法MapPath。 public static string MapPath(string strPath)
线程
调用
方法 System.
Web
.
Http
Context
.
Current
.Server.MapPath()
在多
线程
里面使用
Http
Context
.
Current
,
Http
Context
.
Current
是得到
null
的. 所以在
线程
调用
方法,方法中类里面的System.
Web
.
Http
Context
.
Current
.Server.MapPath() 获取不到对象。 应该这么用: public static string MapPath(string strPath) { if
慎用System.
Web
.
Http
Context
.
Current
每当控制流离开页面派生的
Web
表单上的代码的
时
候,
Http
Context
类的静态属性
Current
可能是有用的。 使用这个属性,我们可以获取当前请求(Request),响应(Response),会话(Session,)和应用程序对象(Application objects)以及请求更多服务。 以下面的代码为例。 private void Page_Load(object sender,
ASP.NET多
线程
下使用
Http
Context
.
Current
为
null
解决方案
问题
一:多
线程
下获取文件绝对路径 当我们使用
Http
Context
.
Current
.Server.MapPath(strPath)获取绝对路径
时
Http
Context
.
Current
为
null
,解决办法如下: /// /// 获得当前绝对路径 /// /// 指定的路径 /// 绝对路径 pu
C# System.
Web
.Caching.Cache类详解 缓存 各种缓存依赖
System.
Web
.Caching.Cache类 缓存 各种缓存依赖,Cache类,是一个用于缓存常用信息的类。
Http
Runtime.Cache以及
Http
Context
.
Current
.Cache都是该类的实例。
C#
111,097
社区成员
642,554
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章