4

Ive got an application that different domains are connected to, instead of copying and modifying each application i use same physical location on hard drive but separate application pools and websites on IIS.

Basically i want to change a theme based on hostname. ie. user comes to "websome.com" gets "websome" theme and user comes to "jamessome.com" gets "jamessome" theme.

I set the theme in web.config "pages" attribute which applies the theme globally to whole website. Is there any way i can modify that setting on fly based on domain use entered from? It is probably possible but what are downsizes and what do you suggest to do with little code in order to simplify solution. As i understand if i edit web.config each time user comes in it will take lots of time which is not that elegant... So any ASP.NET gurus out there can write two lines of code so magic will happen ?

There are few solutions for these problem on the website but this will require me to add code to Page_Init event of every page on the site which is unrealistic.

1 Answer 1

8

Actually, it must be set in Page_PreInit, it won't work if you try to change the theme in Page_Init.

The most common solution is to use a parent class for all your pages. This is a one-time only change and places the logic in the parent class. Instead of inheriting from Page you then inherit from, say, ThemedPage. Inside the class ThemedPage, which inherits from Page itself of course, you can override the Page.OnPreInit method.

You asked for "two lines", it's actually one if you remove the clutter. This is VB:

Public Class ThemedPage
    Inherits Page

    Protected Overrides Sub OnPreInit(ByVal e As System.EventArgs)
        Me.Theme = HttpContext.Current.Request.Url.Host.Replace(".com", "")
        MyBase.OnPreInit(e)
    End Sub
End Class

And instead of this:

Partial Class _Default
    Inherits System.Web.UI.Page

you now write this:

Partial Class _Default
    Inherits ThemedPage

That's all! A one-time search/replace and you're done. For completeness sake, here's the same (only the class) for C#:

// C# version
using System.Web;
using System.Web.UI;

public class ThemedPage : Page
{

    protected override void OnPreInit(System.EventArgs e)
    {
        this.Theme = HttpContext.Current.Request.Url.Host.Replace(".com", "");
        base.OnPreInit(e);
    }
}

Update: added VB code sample
Update: added C# code sample

Note: the theme must exist, otherwise you get an exception: Theme 'ThemeName' cannot be found in the application or global theme directories.. If you want a default theme or no theme if the theme isn't there, wrap it around a try/catch block and use the catch block for setting the default theme.

Sign up to request clarification or add additional context in comments.

7 Comments

Abel, i'm using code on page and it does not generate class for each page like code behind method does. Can i just wrap all inside of script runat=server with partial class with inhiritance ?
This would be a great time to start using code-behind and separate the layout / content from the business logic. You can set the Inherits attribute of @ Page on top of your file to the class you created in the App_Code folder (assuming you created a ThemedPage there).
Side note: a partial class is a different concept then inheritance and through the notion of partial classes you cannot force inheritance, you'll have to write an extra partial class for each page (which is why using the Inheritance attribute is easier).
"Partial" means "Protected" on god's language ?
Thanks Abel... Anyway, i did a test with wrapping try/catch and deleted the theme, somehow i still got an exception System.Web.HttpException of that kind...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.