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,/is returned directly. Program Do not set/
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, in Web applications Code You can use the physical file path of the server-based file to read and write the file. 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.