asp.net path Problem Detailed description _ Practical skills

Source: Internet
Author: User
For example, your project is Webapplication1 (URL is: http://localhost/webapplication1/webform1.aspx)
Request.applicationpath is/webapplication1.
If the site is directly returned to "/";
------------------------------------------------------
~/can be used in controls that require a path to be set, such as Imagebutton,image, and link ascx files, configuration files.
.. /The advantage is that you can not know the name of the folder to know the hierarchy can be
/The advantage is that for the root path, if the local directory and the server directory are the same, it can be used directly/to ensure that the path is correct, which is very rare.
Request.applicationpath Note If there is no virtual directory to return directly/, then you do not set up the program/
Like what
request.applicationpath+ "/images/a.gif" if the site is equal to "//images/a.gif", The wrong
If there is a virtual directory is "Webapplication1/images/a.gif"
No/No.
Request.applicationpath to play the virtual directory name, if the site is returned/
~/is similar, but can only be identified with the service-side space;
/For Directory
./Don't know
.. /relative to the current directory of the Upper directory
For example, your project is Webapplication1 (URL is: http://localhost/webapplication1/webform1.aspx)
Request.applicationpath is webapplication1/.
~/is also webapplication1/.
/It's http://localhost/.
If there is a file under WebForm1.aspx webform2.aspx
You can use it in WebForm1.aspx.
<a href=webform2.aspx>ddd</a>
Or
<a href=/webapplication1/webform2.aspx></a>
Or
<a href=. /webform2.aspx></a>
asp.net Web site path
When working with resources in a Web site, you typically must specify the path to the resource. For example, you can use a URL path to refer to an image file in a page or the URL of a page at another location in a Web site. Similarly, code in a WEB application can read and write files using the physical file path of a server-based file. ASP.net provides methods for referencing resources and determining the path of pages or other resources in an application.
Specify the path to the resource
In many cases, an element or control on a page must reference an external resource, such as a file. ASP.net supports various methods of referencing external resources. Depending on whether you are using a client element or a WEB server control, the selected reference method will vary.
Client Element
Client elements are non-WEB server control elements on the page, and they are passed to the browser as-is. Therefore, when referencing resources from a client element, the path should be constructed according to the Standard Rules for URLs in HTML. You can use a fully qualified URL path (also known as an absolute URL path), or you can use a relative path of various types. For example, if a page contains an IMG element, you can set its Src property by using one of the following paths:
Absolute URL Path. An absolute URL path is useful if you are referencing a resource in another location, such as an external Web site.

The site root relative path, which will be resolved based on the site root rather than the application root directory. The site root relative path is useful if you leave a cross application resource, such as an image or client script file, in a folder in the root directory of the Web site.
This example path assumes that the Images folder is located in the root directory of the Web site.

If your site is http://www.contoso.com, this path resolves to the following form.

Yun_qi_img/sampleimage.jpg below is a relative path that is resolved based on the current page path.

resolves to the relative path of the current page path equivalence.

Note
By default, browsers use the URL of the current page as the baseline for resolving relative paths. However, you can include the HTML base element in the page to specify an alternate base path.
server Control
In ASP.net server controls that reference resources, you can use absolute or relative paths, as is the case with client elements. If you use a relative path, it is resolved relative to the path of the page, user control, or theme that contains the control. For example, suppose the Controls folder contains a user control. The user control contains an Image Web server control, and the ImageUrl property of this server control is set to the following path: Images/sampleimage.jpg.
When the user control runs, the path above resolves to the following form:/controls/images/sampleimage.jpg. This is true regardless of where the page hosting the user control is located.
Attention
In the master page, the path of the resource is parsed based on the path of the content page. For more information, see Overview of ASP.net master pages.
The absolute and relative path references in server controls have the following disadvantages:
Absolute paths are not portable between applications. If you move an application that the absolute path points to, the link is interrupted.
If you move a resource or page to a different folder, it may be difficult to maintain a relative path that takes a client-side element style.
To overcome these disadvantages, ASP.net includes the Web application root operator (~), which can be used when you specify a path in a server control. asp.net resolves the ~ operator to the root directory of the current application. You can use the ~ operator and the folder to specify the path based on the current root directory.
The following example shows the ~ operator used to specify the root-relative path for an image when using Image server controls. In this example, the image file is read directly from the Images folder located in the root directory of the Web application, regardless of where the page is located in the Web site.
<asp:image runat= "Server" id= "Image1"
Imageurl= "~/images/sampleimage.jpg"/> can use the ~ operator in any path-related property in a server control. The ~ operator is recognized only for server controls and is located in server code. The ~ operator cannot be used with the client element.
Attention
For mobile pages only, if your application relies on a cookie-free session or if you may receive a request from a mobile device that requires a cookie-free session, using a tilde ("~") in the path causes an unexpected new session to be created and may lose session data. To set the properties of a mobile control by using a path that contains a tilde character such as ~/path, first resolve the path using the ResolveUrl method, and then assign it to the property.
Determine the physical file path for the current Web site
In your application, you may need to determine the path to a file or other resource on the server. For example, if your application reads and writes text files programmatically, you must provide the full physical path of the file for the method used for reading and writing.
It is not a good idea to hard-code physical file paths (for example, C:\Website\MyApplication) into applications because they will be changed if the application is moved or deployed. However, ASP.net provides you with a way to programmatically get any physical file paths in your application. You can then use the base file path to create the full path to the resource you want. Two of the most common asp.net features used to determine file paths are the properties of HttpRequest objects that return path information, and the MapPath method.
Attention
Physical file paths should not be sent to the client because they may be used by a malicious user to get information about your application.
Determining the path based on the request properties
The following table lists the properties of the HttpRequest object, which help you determine the path to the resources in your application.
The examples listed in the table are based on the following assumptions:
The browser request was made using the following URL: Http://www.contoso.com/MyApplication/MyPages/Default.aspx.
The term "virtual path" refers to the part of the request URL that follows the server identifier; In this example, the virtual path refers to the following path:/myapplication/mypages/default.aspx.
The physical path to the site root directory is: C:\inetpub\wwwroot\MyApplication\.
The physical path contains a folder named Mypages.
Attribute description
Applicationpath
Gets the root path of the current application, regardless of the location in the application where the path is requested. In this example, the property returns the following:/
Currentexecutionfilepath
Gets the virtual path of the current request. It differs from the FilePath property in that Currentexecutionfilepath is correct if the request has been redirected in server code. In this example, the property returns the following:/myapplication/mypages/default.aspx
If you obtain a property in the code that is running as a result of a call to Transfer or Execute, the path reflects the location of the code.
FilePath
Gets the virtual path of the current request. In this example, the property returns the following:/myapplication/mypages/default.aspx
Unlike the Currentexecutionfilepath property, FilePath does not reflect server-side transmissions.
Path
Gets the virtual path of the current request. In this example, the property returns the following:/myapplication/mypages/default.aspx
Physicalapplicationpath
Gets the physical file system path of the root directory of the currently executing application. In this example, the property returns the following: C:\inetpub\wwwroot\
PhysicalPath
Gets the physical file system path that corresponds to the requested URL. In this example, the property returns the following: C:\inetpub\wwwroot\MyApplication\MyPages\default.aspx
Using the MapPath method
The MapPath method returns the full physical path of the virtual path passed to the method. For example, the following code returns the file path to the root of the Web site:
Visual Basic
Dim RootPath as String = Server.MapPath ("~")
C#
String RootPath = Server.MapPath ("~");
Attention
The path passed to the MapPath method must be an application-relative path, not an absolute path.
Control.resolveurl method [C #] See
Control class | Control Member | System.Web.UI Namespaces | TemplateSourceDirectory | Control member (Visual J # Syntax) | Managed Extensions for C + + programming
Requirements
Platform: Windows XP, Windows Professional, Windows Server 2003 series
Language
C#
C++
Jscript
Visual Basic
Show All
Converts a URL to a URL that is available at the requesting 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 property.
return value
The converted URL.
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.