Asp. NET multithreading using HttpContext.Current as a null solution

Source: Internet
Author: User
Tags httpcontext

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.