ASP. NET path problem

Source: Internet
Author: User
Tags httpcontext

Have you ever encountered in the daily work often in the path set when the "~/,./、.. /,/, http://www.cnblogs.com/"Are these symbols confusing?" Occasionally, because of the path of the problem depressed half a day thought it was a procedural problem.

For example, your project is Webapplication1 (URL is: http://localhost/webapplication1/webform1.aspx)
Request.applicationpath is/webapplication1.
If the site is directly returned to the "/";
------------------------------------------------------
~/can be used in controls that need to set the path, 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 consistent, then the direct use/can ensure that the path is correct, this situation is very rare.
Request.applicationpath Note If there is no virtual directory to return directly/, then you do not set the program
Like what
request.applicationpath+ "/images/a.gif" if the site is equal to "//images/a.gif", The wrong
If a virtual directory is "Webapplication1/images/a.gif"
No/Of
Request.applicationpath play the virtual directory name, if the site is returned/
~/similar, but only the service side of the space can be identified;
/For Catalogs
./  current directory
.. /relative to the upper directory of the current directory
For example, your project is Webapplication1 (URL is: http://localhost/webapplication1/webform1.aspx)
Request.applicationpath is webapplication1/.
~/is webapplication1/.
/Is http://localhost/
If there's 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 you use a resource 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 a URL to a page elsewhere 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 a way to reference a resource and determine the path to a page or other resource in the application.


Specify the path to the resource
In many cases, elements or controls on a page must reference external resources, such as files. ASP. NET supports various methods of referencing external resources. Depending on whether you are using a client element or a WEB server control, the reference method chosen will be different.

Client element
Client elements are non-WEB server control elements on the page that are passed to the browser as is. Therefore, when referencing a resource 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 various types of relative paths. For example, if the page contains an IMG element, you can set its Src property using one of the following paths:
The absolute URL path. An absolute URL path is useful if you reference a resource in another location, such as an external Web site.

Site root relative path, This path will be parsed based on the Web site root directory, not the application root directory. Site root relative paths are useful if you keep cross-application resources (such as a sample or client script file) in a folder in the root directory of your Web site.


This example path assumes that the Images folder is located under the Web site root directory.

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

Http://www.contoso.com/Images/SampleImage.jpg The following 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, the browser uses 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 controls
In an ASP. NET server control that references a resource, you can use an absolute path or a relative path, just as the client element does. If a relative path is used, the resolution is relative to the page, user control, or path to the theme that contains the control. For example, suppose a 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 is running, the above path resolves to the following form:/controls/images/sampleimage.jpg. This is the result, 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 to 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 are not portable between applications. If you move an application that points to an absolute path, the link will be 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 element style.
To overcome these drawbacks, ASP. NET includes the Web application root operator (~), which you can use 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 ~ operators and folders together to specify the path based on the current root directory.
The following example shows the ~ operator that is used to specify the root relative path for an image when using the image server control. 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 can be recognized only for server controls and is in server code. The ~ operator cannot be used with client elements.


Attention
Mobile pages only, if your application relies on a cookie-free session or may receive requests from a mobile device that require no cookie sessions, using a tilde ("~") in the path results in unexpected creation of a new session, and possibly loss of conversation data. To set the properties of a mobile control using a path that contains a tilde, such as a ~/path, first resolve the path by using the ResolveUrl method, and then assign it to the property.
Determine the physical file path for the current Web site


In an application, you may need to determine the path to a file or other resource on the server. For example, if your application programmatically reads and writes a text file, you must provide the full physical path to the file for the method used for reading and writing.
It is not a good practice to hard code a physical file path (for example, C:\Website\MyApplication) into an application, because these paths will change if the application is moved or deployed. However, ASP. NET gives you a way to programmatically get the path of any physical file in your application. You can then use the base file path to create the full path to the resource you want. The two most common ASP. NET features used to determine the file path are the properties of the HttpRequest object that returns the path information, and the MapPath method.


Attention
Physical file paths should not be sent to clients because they may be used by malicious users to obtain information about your application.
Determine path based on request properties
The following table lists the properties of the HttpRequest object that 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 Web site root directory is: C:\inetpub\wwwroot\MyApplication\.
The physical path contains a folder named Mypages.


Property Description
Applicationpath
Gets the root directory path of the current application, regardless of where in the application it 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 get a property in a running code 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 attribute, FilePath does not reflect the transport on the server side.


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 to the virtual path that is passed to the method. For example, the following code returns the file path of the root directory 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 methods [C #] See
Control Class | Control Member | System.Web.UI Namespaces | TemplateSourceDirectory | Control Member (Visual J # Syntax) | Managed Extensions for C + + programming
Requirements
Platform: Windows $, Windows XP Professional, Windows Server 2003 series

Language
C#
C++
Jscript
Visual Basic
Show All
Converts the URL to a URL that is available on 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.

Absolute path and relative path:

1.Request.applicationpathDirectory of the current app
In JSP, Applicationpath refers to the current directory of application (applications), ASP. NET is also the meaning of this.
Corresponding--for example, there are two Web application domains on my server that are mockte.com one map to the directory mockte.com/1/another to http://mockte.com/2/
Then mockte.com/1/is the first application of Applicationpath mockte.com/2/is the second application Applicationpath

2.Request.filepathVirtual directories that correspond to IIS
such as URL Http://mockte.com/1/index.html/pathinfo
FilePath =/1/index.html

3.Request.pathThe virtual path of the current request
Path is the concatenation of the FilePath and PathInfo tails. For 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 root directory of the application.
Does not contain a path like c:/, compared to Server.MapPath
Can be understood as a relative path (the Server.MapPath of the contrast is the absolute path)

5.server.mappath (string url), map URLs to physical paths on the server
For example http://mockte.com/1/index.html assume that your application is in C:/iis/mysite
So that's c:/iis/mysite/1/index.html.

Local path converted to URL relative path
private string Urlconvertor (String imagesurl1)
{
String tmprootdir = Server.MapPath (System.Web.HttpContext.Current.Request.ApplicationPath.ToString ());//Get Program root directory
String imagesurl2 = Imagesurl1. Replace (Tmprootdir, ""); Convert to relative path
Imagesurl2 = Imagesurl2. Replace (@ "\", @ "/");
Imagesurl2 = Imagesurl2. Replace (@ "aspx_uc/", @ "");
return IMAGESURL2;
}
Relative paths are converted to server local physical paths
private string Urlconvertorlocal (String imagesurl1)
{
String tmprootdir = Server.MapPath (System.Web.HttpContext.Current.Request.ApplicationPath.ToString ());//Get Program root directory
String imagesurl2 = Tmprootdir + IMAGESURL1. Replace (@ "/", @ "\"); Convert to Absolute path
return IMAGESURL2;
}

1. Use filepath= "/logs/abc.txt", is considered to be the root directory, that is, the page file is located in the drive letter, the default is the C drive, then this path is interpreted as "C:\Logs\abc.txt"

2. Use filepath= "~/logs/abc.txt", which is considered the directory of the server

ASP. NET path problem

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.