A standard problem faced in co-branding or rich email scenarios is how to construct HTML, using a single template, with the brand-specific URIs for the images, links, etc. e.g. embedding the required mast-head image and backgrounds for a brand.

The trick is to use an esoteric tag called Base in the Head of the document. Details about the tag here: http://www.w3.org/TR/html401/struct/links.html#h-12.4 and http://www.w3schools.com/tags/tag_base.asp

So, essentially all URIs are written relative with respect to the BASE tag.

For e.g. an image tag like http://foo.bar.com/images/plur.gif and URL like http://foo.bar.com/plur.html would be implemented as:

<HTML>
<HEAD>
   <TITLE>Plur</TITLE>
   <BASE href=http://foo.bar.com>
</HEAD>
 <BODY>
   <P>Our motto is <A href=”/plur.html” mce_href=”/plur.html”>Plur</A> <img src=”/images/plur.gif“>
</BODY>
</HTML>

Now, you most probably have to dynamically put in that Base href because it is going to be different for different brands.
I usually derive the base URL at run-time like this:

HttpRequest rq = HttpContext.Current.Request;
string baseURL = rq.Url.Scheme + “://”+ rq.Url.Authority + rq.ApplicationPath.TrimEnd(‘/’) + ‘/’;

Note: This assumes that the base href is based on the current Request URL. You get the idea.

[Edit] after some recent research, it is evident that the current webmail providers do not respect HTML and most strip out the HEAD tag, so my recommendation above will not work for email. You will have to resolve the URIs inline for each resource to properly render email; rather unfortunate and even challenging in some scenarios but that is the sad truth.

Now, there are a myriad of ways to get URLs from the Request and the ServerVariables, etc. so I thought I’d share this useful blog post with you: http://www.west-wind.com/weblog/posts/269.aspx

Advertisements