Request object and virtual path in asp.net programming

Source: Internet
Author: User

Programming "~"
(1) if the control on the server side (the control using runat = "server") automatically changes "~" For conversion, if you want to convert HTML controls or code, 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.

 

The code is as follows: Copy code

<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:

The code is as follows: Copy code

Response. Write ("<a href = '" + VirtualPathUtility. ToAbsolute ("~ /A/B .htm ") +" '> Dynamic </a> "+" <br/> ");

Response. Write (VirtualPathUtility. Combine (VirtualPathUtility. AppendTrailingSlash ("~ /A/B ")," c.htm "));

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, such as: E: programmer dark horse programmer exercise aspnetpracticeashx

The code is as follows: Copy code
Response. Write (Request. PhysicalApplicationPath );

(3) Request. PhysicalPath: obtain the physical path of the current Request, such as: E: programmer dark horse programmer exercise aspnetpracticeashx article 7 index. aspx

The code is as follows: Copy code
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.

The code is as follows: Copy code

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

The code is as follows: Copy code
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.

The code is as follows: Copy code
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 dark horse programmer exercise aspnetpracticeashx article 7 index. aspx

The code is as follows: Copy code
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:

The code is as follows: Copy code

Then write the following code on the. ashx page:

The code is as follows: Copy code

Context. Response. ContentType = "image/JPEG ";
 
String fullPath = HttpContext. Current. Server. MapPath ("1.jpg ");
 
Using (System. Drawing. Bitmap bitmap = new System. Drawing. Bitmap (fullPath ))
 
         {
 
Using (System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (bitmap ))
 
             {
 
If (context. Request. UrlReferrer = null) // if you browse directly, there is no URlReferrer
 
                 {
 
// G. Clear (System. Drawing. Color. White );
 
G. drawString ("you cannot view images directly. Please view images on the page", new System. drawing. font ("", 20), System. drawing. brushes. red, 0, 0 );
 
                 }
 
Else if (context. Request. UrlReferrer. Host! = "Localhost ")
 
                 {
 
G. Clear (System. Drawing. Color. White );
 
G. DrawString ("This image is intended for internal use only", new System. Drawing. Font ("", 20), System. Drawing. Brushes. Red, 0, 0 );
 
                 }
 
G. drawString ("Your IP address is:" + context. request. userHostAddress, new System. drawing. font ("", 20), System. drawing. brushes. red, 0, 0 );
 
If (context. Request. UserHostAddress = "127.0.0.1" | context. Request. UserHostAddress = "192.168.0.1 ")
 
                 {
 
// If URLReferrer directly accesses an image, it is null. If it is a request embedded in another webpage, URLReferrer is the URL of the page.
 
G. Clear (System. Drawing. Color. Blue );
 
G. DrawString ("IP blocked", new System. Drawing. Font ("", 20), System. Drawing. Brushes. Red, 0, 0 );
 
                 }
 
             }
 
Bitmap. Save (context. Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );
 
         }

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.