Sunday, April 11, 2010

Relative vs Absolute Path in ASP.NET

Relative and Absolute Path



  • An absolute URL path. An absolute URL path is useful if you are referencing resources in another location
  <img src="http://www.contoso.com/MyApplication/Images/SampleImage.jpg" />

  • A site-root relative path, which is resolved against the site root


     <img src="/Images/SampleImage.jpg" />
If your Web site is http://www.softxen.com, the path would resolve to

http://www.contoso.com/Images/SampleImage.jpg


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



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


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

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

  • 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:image runat="server" id="Image1" ImageUrl="~/Images/SampleImage.jpg" />

No comments:

Post a Comment