Understanding relative and absolute paths in Asp.net

Source: Internet
Author: User

Many people are confused about the relative path and absolute path. I have sorted it out and hope it will help you understand it.

++ ++

1.Request. applicationpath->Current application directory
In JSP, applicationpath refers to the current application (ApplicationProgramIn ASP. NET.
For example, two web application domain names on my server are mockte.com, one mapped to the directory mockte.com/1/ and the other to the http://mockte.com/2/.
Then mockte.com/1/#the applicationpath of the first application is the same as mockte.com/2/#applicationpath of the second application.

2.Request. filepath->Virtual directory corresponding to IIS
Such as URL http://mockte.com/1/index.html/pathinfo
Filepath =/1/index.html

3.Request. Path->The Virtual Path of the current request.
PATH is the concatenation of filepath and pathinfo. Example: URL http://mockte.com/1/index.html/pathinfo
Then Path =/1/index.html/pathinfo

4.Request. mappath (string URL)->Map URLs to virtual directories on IIS
This directory is relative to the application root directory.
Compared with server. mappath, paths such as C:/are not included.
It can be understood as a relative path (the server. mappath in comparison is an absolute path)

5.Server. mappath (string URL)->Map a URL to a physical path on the server
For example, http://mockte.com/1/index.html suppose your application is in C:/IIS/mysite
C:/IIS/mysite/1/index.html

// Convert local paths to relative URLs
Private String Urlconvertor ( String Imagesurl1)
{
String Tmprootdir = Server. mappath (system. Web. httpcontext. Current. Request. applicationpath. tostring ()); // Get the program root directory
String Imagesurl2 = Imagesurl1.replace (tmprootdir, "" ); // Convert to relative path
Imagesurl2 = Imagesurl2.replace ( @" \ " , @" / " );
// Imagesurl2 = imagesurl2.replace (@ "aspx_uc /",@"");
Return Imagesurl2;
}
// Convert the relative path to the local physical path of the server
Private String Urlconvertorlocal ( String Imagesurl1)
{
String Tmprootdir = Server. mappath (system. Web. httpcontext. Current. Request. applicationpath. tostring ()); // Get the program root directory
String Imagesurl2 = Tmprootdir + Imagesurl1.replace ( @" / " , @" \ " ); // Convert to absolute path
Return Imagesurl2;
}


1. filepath = "/logs/abc.txt" is considered as the root directory, that is, the drive letter where the webpage file is located. The default disk is drive C. Here, this path is interpreted as "C: \ logs \ abc.txt"

2. Use filepath = "~ /Logs/abc.txt ", which is considered as the Server Directory

3. Use filepath = "./logs/abc.txt", still under the Server Directory

 

 

++ ++

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.