Themes are a very nice feature in ASP.NET 2.0 to allow personalization and customization of a site very easily. Even without recompile an web administrator would be able to change the look and feel of a site completly by just changing the theme in the web.config (e.g.).
So it would be great if you could use Themes for your MCMS website, wouldn't it?
Unfortunatelly it seems the developers of ASP.NET 2.0 did not have systems like MCMS in mind when they implemented this feature: the way they generate the links to the resources in the Themes folder does not work when an ISAPI filter rewrites the incoming URL as the links are implemented as relative links. E.g. a stylesheet inside a themes folder will be inserted into the rendered output of a webform in the application root in the following way:
<link href="App_Themes/Blue/StyleSheet.css" text="text/css" rel="stylesheet" />
The above tag works perfectly fine inside a normal ASP.NET web application.
Assume a ASP.NET 2.0 web form project in the following virtual path:
/WebFormProject /Webform1.aspx /App_Themes /Blue /StyleSheet.css ...
The browser will request Webform1.aspx using the following URL: http://servername/WebFormProject/Webform1.aspx. The browser will now resolves the relative stylesheet link as http://servername/WebFormProject/App_Themes/Blue/StyleSheet.css which is correct.
Now assume the same situation for a MCMS template project:
/TemplateProject /Template1.aspx /AppThemes /Blue /StyleSheet.css ...
If the browser would request the Template file Template1.aspx in the same way Webform1.aspx was accessed anything would be fine. But MCMS hides the details of the template file behind a friendly URL of a Posting!
So lets assume you have a posting Homepage in a Marketing channel:
/Channels /Marketing /Homepage
Now the browser will request the Homepage template using the following URL: http://servername/Marketing/Homepage.htm. The browser now resolves the relative stylesheet link as http://servername/Marketing/App_Themes/Blue/StyleSheet.css which is NOT correct!
To allow relative links to work correct with MCMS Microsoft implemented a feature in the RobotMetaTags control to which renders a <base...> tag that tells the browser which URL to use as prefix for relative links. So in all situation where a <base...> tag can be used the relative link will work correct.
Unfortunatelly there are a couple of situations where the <base...> tag cannot be used. The main reason for this is that a <base...> tag has to contain the full URL including the protocol and the servername. For the Homepage posting above the <base> tag would look as follows:
In a hosting scenario where a publishing firewall or proxy forwards the request to an internal web server the servername in the <base...> tag will now hold the internal servername rather than the DNS name entered in the browser. If this <base...> tag is now sent back to the client then the client will most likely not be able to resolve this internal name in the <base...> tag and the request to the resources will fail. So in a hosting environment either a proxy or firewall that can do content rewrite is required - means the firewall needs to adjust the link in the <base...> tag to reflect the external DNS name - or the base tag has to be disabled.
So in case you can't use the <base...> you need to have a different way to ensure that the App_Themes folder is accessed with the correct URL. As ASP.NET has no way to configure the way it generates the URL to the themes folder we need to modify this URL after ASP.NET has generated it. But before the page is being rendered. This can be done using a custom HttpModule that registers a new PreRender event to do the modification.
This new PreRender event will have to correct the link
<link href="/TemplateProject/App_Themes/Blue/StyleSheet.css" text="text/css" rel="stylesheet" />
This new link will work in all situations as it
The HttpModule below will do the job and will adjust the reference to the "App_Themes" folder:
It really work marvellously!! saved a lot of time. Thanks Stefan.