Httpcontext. Current is used for encapsulation in thread programming.

Source: Internet
Author: User
In thread programming, the performance can be improved a lot, but we cannot use the request context because the thread is not a request! I have left an image in several places during programming, and I also hope to keep a souvenir for unknown comrades !!!

1. cache: normally. web. httpcontext. current. cache, but the httpcontext we get in the thread. current is null, so we have to use system. web. httpruntime. cache to obtain the cache instance. We can encapsulate a method so that we can call the current cache wherever it is. Code As follows: 1 Public   Class Cacher
2 {
3 Private Cacher (){}
4
5 Private   Static   Readonly Cache;
6
7 Static Cacher (){
8 Httpcontext Context = Httpcontext. Current;
9 If (Context =   Null )
10 Cache = Context. cache;
11 Else
12 Cache = Httpruntime. cache;
13 }
14 }

2. obtain the physical path of a file: we usually use system. web. httpcontext. current. request to obtain the current physical and other related paths or URLs, through the system. web. httpcontext. current. server. mappath method to obtain the physical path of the current file or directory. As in the preceding thread, this problem cannot be solved. We can use the application Program Domain (system. appdomain. currentdomain. basedirectory) to obtain the root directory. 1 Public   Static   String Rootpath (){
2 Return Rootpath ( " / " );
3 }
4
5 Public   Static   String Rootpath ( String Filepath)
6 {
7 String Rootpath = Appdomain. currentdomain. basedirectory;
8 String Separator = Path. directoryseparatorchar. tostring ();
9 Rootpath = Rootpath. Replace ( " / " , Separator );
10 If (Filepath ! =   Null )
11 {
12 Filepath = Filepath. Replace ( " / " , Separator );
13 If (Filepath. Length >   0 ) && Filepath. startswith (separator )) && Rootpath. endswith (separator ))
14 {
15 Rootpath = Rootpath + Filepath. substring ( 1 );
16 }
17 Else
18 {
19 Rootpath = Rootpath + Filepath;
20 }
21 }
22 Return Rootpath;
23 }
24
25 Public   String Physicalpath ( String Path)
26 {
27 Return (Rootpath (). trimend ( New   Char [] {Path. directoryseparatorchar })
+ Path. directoryseparatorchar. tostring () + Path. trimstart ( New   Char [] {Path. directoryseparatorchar }));
28 }
29
30 Public   String Mappath ( String Path)
31 {
32 Httpcontext Context = Httpcontext. Current;
33 If (Context ! =   Null )
34 {
35 Return Context. server. mappath (PATH );
36 }
37 Return Physicalpath (path. Replace ( " / " , Path. directoryseparatorchar. tostring (). Replace ( " ~ " , "" ));
38 }

OK. Now, I caught these two guys first, and I will try again later !!!

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.