For example, your project is Webapplication1 (url: http: // localhost/webapplication1/webform1.aspx)
Request. ApplicationPath is/Webapplication1
If it is a site, the system will directly return "/";
------------------------------------------------------
~ /You can use controls that need to set paths, such as imagebutton and image, as well as links to ascx files and configuration files.
The benefit of ../is that you can know the hierarchy without knowing the folder name.
The advantage of/is that it targets the root path. If the local directory is the same as the server directory, you can directly use/to ensure that the path is correct. This is rare.
Request. ApplicationPath. Note that if no virtual directory exists, the/will be returned directly, so do not set/in your program/
For example
Request. ApplicationPath + "/images/a.gif" if the site is "// images/a.gif", it is incorrect.
If a virtual directory is "Webapplication1/images/a.gif"
No/
Request. ApplicationPath indicates the name of the virtual directory. If it is a site/
~ /Similar, but only the server space can be identified;
/Target directory
./Unknown
../Relative to the upper directory of the current directory
For example, your project is Webapplication1 (url: http: // localhost/webapplication1/webform1.aspx)
Request. ApplicationPath is Webapplication1/
~ /Webapplication1/
/Is http: // localhost/
If webform1.aspx has a file webform2.aspx
You can use this method in webform1.aspx.
<A href = webform2.aspx> ddd </a>
Or
<A href =/webapplication1/webform2.aspx> </a>
Or
<A href = ../webform2.aspx> </a>
ASP. NET Website path
When using resources on a website, you must specify the resource path. For example, you can use a URL path to reference the URL of an image file on a page or a page at another location on a website. Similarly, code in Web applications can read and write files using the physical file path of server-based files. ASP. NET provides a method for referencing resources and determining the paths of pages or other resources in an application.
Path of the specified resource
In many cases, elements or controls on a page must reference external resources, such as files. ASP. NET supports various methods to reference external resources. The reference method selected varies depending on whether you are using the client element or Web Server Control.
Client Element
Client elements are non-Web Server Control Elements on the page, which are passed to the browser as they are. Therefore, when referencing resources from the client element, the path should be constructed according to the standard URL rules in HTML. You can use a fully-qualified URL path (also known as an absolute URL path) or various types of relative paths. For example, if the page contains an img element, you can use one of the following paths to set its src attribute:
Absolute URL path. If you reference resources in other locations (such as external websites), the absolute URL path is very useful.
relative path of the website root directory, which is parsed Based on the website root directory rather than the application root directory. If you keep cross-application resources (such as client script files) in the folder under the root directory of the website, the relative path of the root directory of the website is very useful.
The path in this example assumes that the Images folder is located under the root directory of the website.
If your website is a http://www.contoso.com, this path resolves to the form below.
Http://www.contoso.com/images/sampleimage.jpgthe following figure shows the relative content of the previous page.
parses the file into a relative path equivalent to the current page path.
note
By default, the browser uses the URL of the current page as the benchmark for parsing relative paths. However, you can include the HTML base element in the page to specify an alternative base path.
Server Control
In the ASP. NET Server Control that references resources, you can use an absolute or relative path, which is the same as the client element. If the relative path is used, it is parsed relative to the path of the page, user control, or topic containing the control. For example, assume that the Controls folder contains a user control. This user control contains an Image Web Server Control. The ImageUrl attribute of this server control is set to the following path: Images/SampleImage.jpg.
When the user control is running, the above path will be resolved to the following form:/Controls/Images/SampleImage.jpg. The results are the same no matter where the page that hosts the user control is located.
Note:
On the master page, the resource path is parsed Based on the path on the Content Page. For more information, see ASP. NET master page overview.
Absolute and relative path references in server controls have the following Disadvantages:
Absolute paths cannot be transplanted between applications. If you move the absolute path to the application, the link will be interrupted.
If you move resources or pages to different folders, it may be difficult to maintain the relative path using the client element style.
To overcome these disadvantages, ASP. NET includes the Web application root directory operator (~), This operator can be used when you specify a path in the server control. ASP. NET will ~ The operator is resolved to the root directory of the current application. Can be used in combination ~ Operator and folder to specify the path based on the current root directory.
The following example demonstrates how to use the Image server control to specify the relative path of the root directory for the Image ~ Operator. In this example, the image file is directly read from the Images folder in the Web application root directory no matter where the page is located on the website.
<Asp: image runat = "server" id = "Image1"
ImageUrl = "~ /Images/SampleImage.jpg "/> can be used in any path-related attribute in the server control ~ Operator .~ The operator can only be recognized by the server control and is located in the server code. Cannot change ~ The operator is used for client elements.
Note:
Only for mobile pages. If your application depends on a non-Cookie Session or you may receive a request from a mobile device that requires a non-Cookie Session, use the waveform character ("~") in the path. A new session may be created accidentally and session data may be lost. To use a waveform character (for example, "~ /Path) to set the property of the mobile control. Use the ResolveUrl method to parse the path and then assign it to the property.
Determine the physical file path of the current website
In an application, you may need to determine the path of files or other resources on the server. For example, if an application reads or writes a text file programmatically, the complete physical path of the file must be provided for the Read and Write methods.
Hard coding of physical file paths (such as C: \ Website \ MyApplication) to applications is not a good practice, because these paths will be changed if you move or deploy applications. However, ASP. NET provides you with a way to obtain any physical file path in an application programmatically. Then, you can use the base file path to create the complete path of the required resources. The two most common ASP. NET functions used to determine the file path are the attributes of the HttpRequest object that returns the path information and the MapPath method.
Note:
Physical file paths should not be sent to the client because they may be used by malicious users to obtain information about your applications.
Determine the path based on the request property
The following table lists the attributes of the HttpRequest object. These attributes help you determine the path of resources in the application.
The examples listed in the table are based on the following assumptions:
You have used the following URL to send a browser request: http://www.contoso.com/myapplication/mypages/default.aspx.
The term "virtual path" refers to the part of the request URL following the server identifier. In this example, the virtual path refers to the following path:/MyApplication/MyPages/Default. aspx.
The physical path of the website root directory is: C: \ inetpub \ wwwroot \ MyApplication \.
This physical path contains a folder named MyPages.
Attribute description
ApplicationPath
Obtain the root directory path of the current application. In this example, the property returns the following content :/
CurrentExecutionFilePath
Obtain the virtual path of the current request. Unlike the FilePath attribute, CurrentExecutionFilePath is correct if the request has been redirected in the server code. In this example, the property returns the following content:/MyApplication/MyPages/Default. aspx
If you obtain the properties in the running code as a result of calling Transfer or Execute, the path will reflect the location of the Code.
FilePath
Obtain the virtual path of the current request. In this example, the property returns the following content:/MyApplication/MyPages/Default. aspx
Unlike the CurrentExecutionFilePath attribute, FilePath does not reflect transmission on the server.
Path
Obtain the virtual path of the current request. In this example, the property returns the following content:/MyApplication/MyPages/default. aspx
PhysicalApplicationPath
Obtain the physical file system path of the root directory of the currently running application. In this example, the property returns the following content: C: \ inetpub \ wwwroot \
PhysicalPath
Obtain the physical file system path corresponding to the requested URL. In this example, the property returns the following content: C: \ inetpub \ wwwroot \ MyApplication \ MyPages \ default. aspx
Use the MapPath Method
The MapPath method returns the complete physical path of the virtual path passed to the method. For example, the following code returns the file path of the root directory of the website:
Visual Basic
Dim rootPath As String = Server. MapPath ("~ ")
C #
String rootPath = Server. MapPath ("~ ");
Note:
The path passed to the MapPath method must be the relative path of the application, rather than the absolute path.
For the Control. ResolveUrl method [C #], see
Control | Control member | System. Web. UI namespace | TemplateSourceDirectory | Control member (Visual J # syntax) | C ++ managed extended Programming
Requirements
Platform: Windows 2000, Windows XP Professional, Windows Server 2003 Series
Language
C #
C ++
JScript
Visual Basic
Show all
Convert a URL to a URL available on the request client.
[Visual Basic]
Public Function ResolveUrl (_
ByVal relativeUrl As String _
) As String
[C #]
Public string ResolveUrl (
String relativeUrl
);
[C ++]
Public: String * ResolveUrl (
String * relativeUrl
);
[JScript]
Public function ResolveUrl (
RelativeUrl: String
): String;
Parameters
RelativeUrl
The URL associated with the TemplateSourceDirectory attribute.
Return Value
The converted URL.
Detailed source reference: http://www.jb51.net/article/18622.htm