Problem one: Get file absolute path under multithreading
Problem one: Get file absolute path under multithreading
When we use HttpContext.Current.Server.MapPath (strpath) to obtain an absolute path httpcontext.current is null, the workaround is as follows:
/// ///get the current absolute path/// ///the specified path///Absolute Path PublicStaticstringGetmappath (stringstrpath) { if(Strpath.tolower (). StartsWith ("/ http")) { returnstrpath; } if(HttpContext.Current! =NULL) { returnHttpContext.Current.Server.MapPath (strpath); } Else//non-web program reference{strpath= Strpath.replace ("/","\\"); if(Strpath.startswith ("\\") || Strpath.startswith ("~") ) {strpath= Strpath.substring (Strpath.indexof ('\\',1)). TrimStart ('\\'); } returnSystem.IO.Path.Combine (AppDomain.CurrentDomain.BaseDirectory, strpath); } }
Problem two: Getting cache problems under multi-threading
HttpContext.Current is null when using HttpContext.Current.Cache.Get (key) to get the cache under multithreading, the workaround is as follows:
HttpRuntime.Cache.Get (key);
As can be seen from the explanation on MSDN, Httpruntime.cache is application-level and HttpContext.Current.Cache is defined for the current web context.
However, in fact, these two are all called the same object, the difference is: httpruntime In addition to the Web can be used outside, non-web programs can also be used.
HttpContext can only be used in the Web. So, wherever possible, we use httpruntime as much as possible (however, how to invoke between different applications is also a problem).
Problem three: Using HTML transcoding under multithreading
The workaround for using HttpContext.Current.Server.HtmlEncode (htmlstring) transcoding httpcontext.current is null under Multithreading:
Httputility.htmlencode (htmlstring)
In short, HttpContext is not omnipotent, when the multi-threaded call, or use the machine simulation call, at this time there is no HttpContext context.
Asp. NET multithreading using HttpContext.Current as a null solution