views:

470

answers:

3

I have a web app that up until now has been skinned (just basic colours and logos, nothing complicated) to a single company , however now since the merging with another company the site needs to be branded as two seperate companies (operation is exactly the same for both, and they share the same data). The simplest way would be to just copy the web app and host two instances of it, but that will be a maintenance hassle, I really just want to setup a DNS alias to the same site.

Basically I want to change the theme based on the URL of the site. e.g. alpha.company.com -> Theme A beta.comany.com -> Theme B.

How would you recommend to solve this?

A: 

In MasterPage.PreInit event use:

Page.Theme = (Request.RawUrl.Contains("...") ? "yellow": "blue");

Or something along those lines...

Hope this helps, Florin.

Florin Sabau
MasterPage does not have a PreInit method; also, the RawUrl does not contain the host header portion of the request, only the path (e.g. "/application/hello.aspx")
Rex M
PreInit is an event and if MasterPage is derived from System.Web.UI.Page it will contain that event. Request.RawUrl indeed doesn't contain the host part but that's easy to get from Request.Url.Host or Request.Url.GetLeftPart(...).
Florin Sabau
MasterPage does not derive from Page, it derives from UserControl.
Rex M
And just to be clear about things, "Or something along those lines" means I didn't compile it or looked it up on google, just dumped the code from the top of my head. It may have small gotchas, but the idea is there.
Florin Sabau
+2  A: 

In your page (or base page), get on the PreInit handler (only Page has this event, not MasterPage) and do something like the following:

protected void Page_PreInit(..)
{
    this.Theme = GetThemeByUrl(Request.Url);
}

private string GetThemeByUrl(Uri url)
{
    string host = url.Host; //gets 'subdomain.company.com'
    //determine & return theme name from host
}
Rex M
If you need to do this for each page, it is easier to use a common base class derived from `Page` and override `OnPreInit`, see the example in this related post: http://stackoverflow.com/questions/1600109/dynamically-setting-themes-in-asp-net
Abel
A: 

Best way would be to override Theme property in page class :

Check this ASP.NET Themes and Right-to-Left Languages

public override string Theme
{
    get
    {
        if (!string.IsNullOrEmpty(base.Theme))
        {
            return (CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft ? string.Format("{0}.rtl", base.Theme) : base.Theme);
        }
        return base.Theme;
    }
    set
    {
        base.Theme = value;
    }
}
MK