多线程获取不到HttpContext
2016-03-31 18:56
148 查看
问题一:多线程下获取文件绝对路径
当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办法如下:
[c#] view plain copy print?
///
/// 获得当前绝对路径
///
/// 指定的路径
/// 绝对路径
public static string GetMapPath(string strPath)
{
if (strPath.ToLower().StartsWith("http://"))
{
return strPath;
}
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("\\") || strPath.StartsWith("~"))
{
strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}
问题二:多线程下获取缓存问题
多线程下使用HttpContext.Current.Cache.Get(key)获取缓存时HttpContext.Current为null,解决办法如下:
HttpRuntime.Cache.Get(key);
从MSDN上的解释可以看出,HttpRuntime.Cache是应用程序级别的,而HttpContext.Current.Cache是针对当前WEB上下文定义的。
然而,实际上,这二个都是调用的同一个对象,不同的是:HttpRuntime下的除了WEB中可以使用外,非WEB程序也可以使用。
而HttpContext则只能用在WEB中。因此,在可能的情况下,我们尽可能使用HttpRuntime(然而,在不同应用程序之间如何调用也是一个问题)。
问题三:多线程下使用Html转码问题
多线程下使用HttpContext.Current.Server.HtmlEncode(Htmlstring)转码HttpContext.Current为null,解决办法如下:
HttpUtility.HtmlEncode(Htmlstring)
总之,HttpContext不是万能的,当多线程调用,或是用机器模拟调用时,此时是没有HttpContext上下文的。
详细讲解可以看下这篇文章 http://www.cnblogs.com/fish-li/archive/2011/08/21/2148640.html#_label4
当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办法如下:
[c#] view plain copy print?
///
/// 获得当前绝对路径
///
/// 指定的路径
/// 绝对路径
public static string GetMapPath(string strPath)
{
if (strPath.ToLower().StartsWith("http://"))
{
return strPath;
}
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("\\") || strPath.StartsWith("~"))
{
strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}
问题二:多线程下获取缓存问题
多线程下使用HttpContext.Current.Cache.Get(key)获取缓存时HttpContext.Current为null,解决办法如下:
HttpRuntime.Cache.Get(key);
从MSDN上的解释可以看出,HttpRuntime.Cache是应用程序级别的,而HttpContext.Current.Cache是针对当前WEB上下文定义的。
然而,实际上,这二个都是调用的同一个对象,不同的是:HttpRuntime下的除了WEB中可以使用外,非WEB程序也可以使用。
而HttpContext则只能用在WEB中。因此,在可能的情况下,我们尽可能使用HttpRuntime(然而,在不同应用程序之间如何调用也是一个问题)。
问题三:多线程下使用Html转码问题
多线程下使用HttpContext.Current.Server.HtmlEncode(Htmlstring)转码HttpContext.Current为null,解决办法如下:
HttpUtility.HtmlEncode(Htmlstring)
总之,HttpContext不是万能的,当多线程调用,或是用机器模拟调用时,此时是没有HttpContext上下文的。
详细讲解可以看下这篇文章 http://www.cnblogs.com/fish-li/archive/2011/08/21/2148640.html#_label4
相关文章推荐
- 多级神经网络结构表达文档语义性能更好吗
- 自动检测iOS网络并可跳转至设置界面设置网络
- RST包 - TCP/IP
- http和https有什么区别?
- nyoj 120 校园网络(求添加多少条边使整个图强连通)
- 26 API-网络编程(网络概述,Socket通信机制,UDP协议发送和接收数据,TCP协议发送和接收数据)
- {百度竞价}专题页面如何策划你懂吗?【太原网络营销师】讲
- HTTP in iOS你看我就够
- Http请求中Content-Type讲解
- http报头参数详解
- http请求及缓存框架 GalHttprequest
- 正确使用AFNetworking的SSL保证网络安全
- TCP三次握手四次挥手
- url参数中带有&号,需要用先把url做个解析,使其方便在网络上传递
- Java核心技术(第8版)学习笔记_网络
- android 内置webview 对https的支持
- Http keep-alive 与Tcp keep-alive
- 解决svn一直报Error validating server certificate for https://XXXX fingerprint
- 在Xcode7/7.1中使用Http请求
- TCP/IP学习笔记:TCP传输控制协议(一)