Monday, October 02, 2006

ASP.net Website Paths

Determining Physical Path of an Application

The MapPath method returns the complete physical path for a virtual path that you pass to the method. For example, the following code returns the file path for the root of your Web site:
String rootPath = Server.MapPath("~");


Client Elements
Elements that are not Web server controls on a page—client elements—are passed through as-is to the browser. Therefore, when referring to a resource from a client element, you construct paths according to standard rules for URLs in HTML. You can use a fully qualified (which is also known as absolute) URL path or various types of relative paths. For example, if your page contains an img element, you can set its src attribute using one of the following paths:

1) An absolute URL path. An absolute URL path is useful if you are referencing resources in another location, such as an external Web site.
img src="http://www.yahoo.com/MyApplication/Images/SampleImage.jpg"

2) A site-root relative path, which is resolved against the site root (not the application root). Site-root relative paths are useful if you keep cross-application resources, such as images or client script files, in a folder that is located under the Web site root.

This example path assumes that an Images folder is located under the Web site root.
img src="/Images/SampleImage.jpg"

Here, If your Web site is http://www.yahoo.com, the path would resolve to the following.
http://www.yahoo.com/Images/SampleImage.jpg

3) A relative path that is resolved against the current page path.
img src="Images/SampleImage.jpg"

4) A relative path that is resolved as a peer of the current page path.
img src="../Images/SampleImage.jpg"



Server Controls

Absolute and relative path references in a server control have the following disadvantages:

i) Absolute paths are not portable between applications. If you move the application that the absolute path points to, the links will break.

ii) Relative paths in the style of client elements can be difficult to maintain if you move resources or pages to different folders.

To overcome these disadvantages, ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root.

The following example shows the ~ operator used to specify a root-relative path for an image when using the Image server control In this example, the image file is read from the Images folder that is located directly under the root of the Web application, regardless of where in the Web site the page is located.
asp:image runat="server" id="Image1" ImageUrl="~/Images/SampleImage.jpg"

4 comments:

cruSher said...

Thank you very much. This tip saved me a lot of time.

DotNetGuts said...

Physical Server Path in asp.net
string strServerPhysicalPath = Server.MapPath("~");

Logical Server Path in asp.net
string strServerLogicalPath = Page.ResolveUrl("~");

DotNetGuts said...

Image Display Problem in Master Pages can be resolved with following trick

Master Page Image Display Problem Resolution in Asp.net

Simon said...

Had so many problems trying to sort paths out!

Now i have the wonderfull Page.ResolveUrl() in my bag.

Im now the King of the world

Thanks guys for saving me some heart ache

Most Recent Post

Community Updates

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape