ASP. NET advanced programming basics Article 8-request object and virtual path

Source: Internet
Author: User

This article describes virtual paths and request objects, which are commonly used in Asp.net, we hope that by reading this blog, we can learn more about these knowledge points, especially the virtual path "~" And some properties of the request object.

    1. Special path mark "~" Virtual Path

(1) "/indicates the website root directory (domain name),.../indicates the parent directory,./indicates the current directory" and other HTTP standard positioning are different ,~ It is a special symbol defined by ASP. NET and is recommended for use within ASP. NET. It is used for recommendation resource positioning ~ As defined from the application root directory, the difference between the application root directory and the website root directory is:ProgramDeploy to deployment. Therefore, it is best to use "~","~" It is not recognized by the browser, so ASP. NET will convert this path to the full path relative to the root directory of the website and output it to the browser.

    1. Programming "~"

(1) If the control on the server side (the control using runat = "server") automatically changes "~" For conversion, if the HTML control orCodeFor conversion, you can use static methods in the virtualpathutility class to convert virtual paths and full paths, such as virtualpathutility. toabsolute ("~ /A/B. aspx ") is to convert the virtual path to the full path corresponding to the application root directory, that is," website/a/B. aspx ".

(2) Main Methods of the virtualpathutility class

1) string appendtrailingslash (string virtualpath); if the path virtualpath does not last "/", add it.

2) string combine (string basepath, string relativepath); merge the two paths.

3) string getdirectory (string virtualpath); returns the directory part of the virtual path.

4) string makerelative (string frompath, string topath); Calculate the relative paths of the two virtual paths.

5) toabsolute: converts to an absolute path.

(3) Example: create a web project named path. aspx.

<A href = "/a.htm"> A </a> & nbsp;

<A href = "~ /B .htm "> B </a> & nbsp;

<Asp: hyperlink id = "hyperlink1" runat = "server" navigateurl = "/a.htm"> CE1 </ASP: hyperlink> & nbsp;

<Asp: hyperlink id = "hyperlink2" runat = "server" navigateurl = "~ /B .htm "> CE2 </ASP: hyperlink>

Drag and Drop two hyperlink controls, and name the text of the two controls CE1 and CE2. Assign the navigateurl of the two controls to "/B .htm ","~ /B .htm ". Write the following code under the page_load event:

Response. Write ("<a href = '" + virtualpathutility. toabsolute ("~ /A/B .htm ") +" '> dynamic </a> "+" <br/> ");

Response. Write (virtualpathutility. Combine (virtualpathutility. appendtrailingslash ("~ /A/B ")," c.htm "));

    1. Request object

(1) Request. apprelativecurrentexecutionfilepath to obtain the virtual path of the current execution request relative to the application root directory, "~" For example :"~ /Article 7/index. aspx ".

Response. Write (request. apprelativecurrentexecutionfilepath );

(2) request. physicalapplicationpath to obtain the physical path of the current application, for example, E: \ programmer \ programmer exercise \ aspnetpractice \ ashx \

Response. Write (request. physicalapplicationpath );

(3) request. physicalpath to obtain the physical path of the current request, for example, E: \ programmer \ programmer exercise \ aspnetpractice \ ashx \ Article 7 \ index. aspx

Response. Write (request. physicalpath + "<br/> ");

(4) request. rawurl to obtain the original request URL, such as/ashx/7th/index. aspx, request. obtain the requested URL, such as http: // localhost: 2602/ashx/7th/index. aspx. The difference involves URL rewriting.

Response. Write (request. rawurl + "<br/> ");

Response. Write (request. url + "<br/> ");

(5) request. the source of the urlreferrer webpage can be used to determine the keyword found from Baidu, prevent downloading leeching, and put the image leeching. This image can be forged and used only for internal communication in the blog garden ", globals is used for global anti-leech protection. asax.

(6) request. userhostaddress: Obtain the visitor's IP address

Response. Write (request. userhostaddress + "<br/> ");

(7) request. userlanguage: Obtain the language supported by the visitor's browser. You can use this method to display different pages for people in different languages.

Response. Write (request. userages + "<br/> ");

(8) request. cookies obtain the cookie sent from the browser and read the cookie value from it, such as context. request. cookies ["mysessionid"], using request. generally, the cookie is read only. Write the cookie back to the browser and use response. cookies.

(9) request. mappath (virtualpath) converts a virtual path to a physical path on the disk. Request. mappath (~ /Article 7/index. aspx): e: \ programmer \ programmer exercise \ aspnetpractice \ ashx \ Article 7 \ index. aspx

Response. Write (request. mappath ("~ /Article 7/index. aspx "));

(10) create a folder request object, create a common processing program, create an HTML page, and write the following code in the HTML page:

Then write the following code on the. ashx page:

 1 Context. response. contenttype = " Image/JPEG  "  ;  2   3           String Fullpath = httpcontext. Current. server. mappath ( "  1. jpg  "  );  4   5           Using (System. Drawing. Bitmap bitmap = New  System. Drawing. Bitmap (fullpath )) 6   7   {  8   9               Using (System. Drawing. Graphics G = System. Drawing. Graphics. fromimage (Bitmap ))  10   11   {  12   13                   If (Context. Request. urlreferrer = Null ) // If you directly browse, there is no urlreferrer  14   15   {  16   17                       //  G. Clear (system. Drawing. color. White );  18   19 G. drawstring ( "  You cannot view images directly. Please view them on the page.  " , New System. Drawing. Font ( "   " , 20 ), System. Drawing. Brushes. Red, 0 , 0  );  20   21   }  22   23                   Else   If (Context. Request. urlreferrer. Host! = "  Localhost "  )  24   25   {  26   27   G. Clear (system. Drawing. color. White );  28   29 G. drawstring ( "  This image is for internal use only.  " , New System. Drawing. Font ( "   " , 20 ), System. Drawing. Brushes. Red, 0 , 0  );  30   31   }  32   33 G. drawstring ( "  Your IP address is:  " + Context. Request. userhostaddress, New System. Drawing. Font ( "    " , 20 ), System. Drawing. Brushes. Red, 0 , 0  );  34   35                   If (Context. Request. userhostaddress = "  127.0.0.1  " | Context. Request. userhostaddress = " 192.168.0.1  "  )  36   37   {  38   39                       //  If you directly access the image urlreferrer is null, if it is embedded in another webpage request, urlreferrer is the page address  40   41   G. Clear (system. Drawing. color. Blue );  42   43 G. drawstring ("  IP Address blocked  " , New System. Drawing. Font ( "    " , 20 ), System. Drawing. Brushes. Red, 0 , 0  );  44   45   }  46   47  }  48   49   Bitmap. Save (context. response. outputstream, system. Drawing. imaging. imageformat. JPEG );  50   51 }

Note: Here is the request object and virtual path. The next blog is the response object and Server Object.

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.