URLs in master page of asp.net 2.0

(from : http://www.odetocode.com/Articles/450.aspx )
At runtime, the master page and the content page are in the same control hierarchy – the master page is essentially a user control inside the content page. At design time, however, the master page and content page are two different entities. In fact, the master page and content page may live in different directories. During design time, it’s easy to put URLs and relative paths into our master pages, but we have to be careful when using relative paths. Take the following master page excerpt as an example:.

<div>
<img src=”logo.gif” alt=”Company Logo” />

<asp:ContentPlaceHolder ID=”ContentPlaceHolder1″ runat=”server”>
</asp:ContentPlaceHolder>
</div>

The good news is, the ASP.NET runtime does provide a feature called “URL rebasing”. The runtime will try to “rebase” relative URLs it finds on server-side controls inside a master page. This means the following relative path will work, no matter where the master page and web form live.

<img src=”logo.gif” alt=”Company Logo” runat=”server” />

We’ve added a runat=”server” attribute to the image tag, making the <img> a server-side control. When the master page file and logo are in the root directory, but the web form is in a subdirectory, the ASP.NET runtime will rebase the relative path it finds in the src attribute to point to the root of the website.

The following code will also work, because we are using a server-side Image object.

<asp:Image ImageUrl=”logo.gif” runat=”server” />

The ASP.NET runtime will also rebase paths it finds inside of the head tag. Take the following excerpt from a master page:

<head runat=”server”>
<title>Untitled Page</title>

<link href=”styles/styles.css” type=”text/css” rel=”stylesheet”/>

</head>

If we request a webform from a subdirectory, the runtime will catch the href inside the link tag and rebase the URL to “../styles/styles.css”. However, the runtime doesn’t catch everything. If we included our style sheet with the following code, the runtime won’t rebase the relative href.
<head runat=”server”>

<style type=”text/css” media=”all”>
@import “styles/styles.css”;
</style>

</head>

Also, the runtime doesn’t rebase URLs inside of embedded styles, and not all attributes are covered (the background attribute, for instance).

<body background=”logo.gif” runat=”server”>
<!– the background for the body tag will break –>
<form id=”form1″ runat=”server”>

<div id=”Div1″ style=”background-image: url(‘logo.gif’);” runat=”server”>
<!– My background is also broken. –>
</div>

If you need to use a relative path in an area where the runtime does not provide the rebasing feature, you can compute a client side URL using ResolveClientUrl and passing a relative path. ResolveClientUrl, when called from inside a master page, will take into account the location of the master page, the location specified in the HTTP request, and the location specified by the relative path parameter to formulate the correct relative path to return.

<body background=<%= ResolveClientUrl(“logo.gif”) %> >

When working with image paths in embedded styles, it’s often a good idea to move the style definition into a .css file. The ASP.NET runtime will rebase the path it finds inside a link tag, so we won’t have any problems locating the stylesheet from any webform. Take the following style definition in a .css file:

body
{
background-image:url(‘images\logo.gif’);
}

Relative paths are safe inside a .css file because the browser will always request logo.gif relative to the location of the stylesheet.