Rosstified
Rosstified

Reputation: 4087

ASP.NET set theme based on URL

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?

Upvotes: 3

Views: 1650

Answers (3)

MK.
MK.

Reputation: 5149

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;
    }
}

Upvotes: 0

Rex M
Rex M

Reputation: 144112

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
}

Upvotes: 5

Florin Sabau
Florin Sabau

Reputation: 1075

In MasterPage.PreInit event use:

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

Or something along those lines...

Hope this helps, Florin.

Upvotes: -3

Related Questions