Art Of Programming

musings by Dmytrii Nagirniak

ASP.NET MasterPages and Url Rebasing

Always learn something new. I really didn’t know that.
I paid some attention to my MasterPage and the link to CSS file. Here is what I noticed.

The default Site.Master has this markup:
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>
I expected the link href will always be static as it is set: ”../../Content/Site.css”
It means pages like these would have the CSS:
  • Home/One/Index.aspx
  • Home/Two/Index.aspx
  • Product/One/Edit.aspx
and I expected following ones would not have CSS because of broken link:
  • Home/Index.aspx
  • Home/Two/Three/Index.aspx
  • Product/Category/One/Edit.aspx
but I wondered that the HREF of the link has always been generated correctly!
How come?
The answer is Url Rebasing - the feature of the master pages.
The runtime will try to “rebase” relative URLs it finds on server-side controls inside a master page and paths it finds inside of the head tag.

Comments