November 10, 2006

Does this dress make me look fat?

Here's a nice little bit of code that can be used to dynamically theme a website that has multiple domains at the same site.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;

namespace URLThemeHandler {
public class ThemeHandler : IHttpModule {
#region IHttpModule Members

public void Dispose() {

public void Init(HttpApplication context) {
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);

void context_PreRequestHandlerExecute(object sender, EventArgs e) {
HttpContext currentContext = HttpContext.Current;
if (!(currentContext.Handler is System.Web.UI.Page)) return;
System.Web.UI.Page myPage = currentContext.Handler as System.Web.UI.Page;
string strHost = currentContext.Request.Url.Host;
myPage.Theme = strHost;

bool ThemeExists(string theme) {
string strPath = HttpContext.Current.Server.MapPath("~/App_Themes/" + theme);
return System.IO.Directory.Exists(strPath);


You can register it in the httpModules section of the Web.config.