Click here to Skip to main content
Click here to Skip to main content

ASP.NET MVC Dynamic Themes

By , 26 Jan 2009
 

Introduction

I really needed to enable themes for my application, and I found an interesting article about it by Chris Pietschmann. In my point of view, the only problem with his approach is that you need to repeat each page for all themes. Well, I only want to create one page per theme, and have a master page and CSS files for each theme.

Using the code

For this project, I made some assumptions:

  • The master page for each theme has the name Site.Master.
  • The master page file and CSS files will be placed on a folder with the name of the theme.
  • All the theme folders will be placed in the Content folder.

The file structure will be like this:

Now, let’s write a custom View Engine that will inherit from WebFormViewEngine. This code is only a sketch of what I want to implement. For instance, I want to save the user theme selection so that, in the next login, the user has his option set.

public class ThemeViewEngine : WebFormViewEngine
{
    public ThemeViewEngine()
    {
        base.ViewLocationFormats = new string[] {
            "~/Views/{1}/{0}.aspx",
            "~/Views/{1}/{0}.ascx",
            "~/Views/Shared/{0}.aspx",
            "~/Views/Shared/{0}.ascx"
        };

        base.MasterLocationFormats = new string[] {
            "~/Content/{2}/Site.master"
        };

        base.PartialViewLocationFormats = new string[] {
            "~/Views/{1}/{0}.aspx",
            "~/Views/{1}/{0}.ascx",
            "~/Views/Shared/{0}.aspx",
            "~/Views/Shared/{0}.ascx"
        };
    }

    public override ViewEngineResult FindView(ControllerContext controllerContext, 
                    string viewName, string masterName)
    {
        if (controllerContext == null)
        {
            throw new ArgumentNullException("controllerContext");
        }
        if (string.IsNullOrEmpty(viewName))
        {
            throw new ArgumentException("Value is required.", "viewName");
        }

        string themeName = this.GetThemeToUse(controllerContext);

        string[] searchedViewLocations;
        string[] searchedMasterLocations;

        string controllerName = 
          controllerContext.RouteData.GetRequiredString("controller");

        string viewPath = this.GetViewPath(this.ViewLocationFormats, viewName, 
                          controllerName, out searchedViewLocations);
        string masterPath = this.GetMasterPath(this.MasterLocationFormats, viewName, 
                            controllerName, themeName, out searchedMasterLocations);

        if (!(string.IsNullOrEmpty(viewPath)) && 
           (!(masterPath == string.Empty) || string.IsNullOrEmpty(masterName)))
        {
            return new ViewEngineResult(
                (this.CreateView(controllerContext, viewPath, masterPath)), this);
        }
        return new ViewEngineResult(
          searchedViewLocations.Union<string>(searchedMasterLocations));
    }

    public override ViewEngineResult FindPartialView(ControllerContext controllerContext, 
                                                     string partialViewName)
    {
        if (controllerContext == null)
        {
            throw new ArgumentNullException("controllerContext");
        }
        if (string.IsNullOrEmpty(partialViewName))
        {
            throw new ArgumentException("Value is required.", partialViewName);
        }

        string themeName = this.GetThemeToUse(controllerContext);

        string[] searchedLocations;

        string controllerName = controllerContext.RouteData.GetRequiredString("controller");

        string partialPath = this.GetViewPath(this.PartialViewLocationFormats, 
                             partialViewName, controllerName, out searchedLocations);

        if (string.IsNullOrEmpty(partialPath))
        {
            return new ViewEngineResult(searchedLocations);
        }
        return new ViewEngineResult(this.CreatePartialView(controllerContext, 
                                    partialPath), this);
    }

    private string GetThemeToUse(ControllerContext controllerContext)
    {
        if (controllerContext.HttpContext.Request.QueryString.AllKeys.Contains("theme"))
        {
            string themeName = controllerContext.HttpContext.Request.QueryString["theme"];
            controllerContext.HttpContext.Session.Add("Theme", themeName);
        }
        else if (controllerContext.HttpContext.Session["Theme"] == null)
        {
            controllerContext.HttpContext.Session.Add("Theme", "Default");
        }
        return controllerContext.HttpContext.Session["Theme"].ToString();
    }

    private string GetViewPath(string[] locations, string viewName, 
                   string controllerName, out string[] searchedLocations)
    {
        string path = null;

        searchedLocations = new string[locations.Length];

        for (int i = 0; i < locations.Length; i++)
        {
            path = string.Format(CultureInfo.InvariantCulture, locations[i], 
                                 new object[] { viewName, controllerName });
            if (this.VirtualPathProvider.FileExists(path))
            {
                searchedLocations = new string[0];
                return path;
            }
            searchedLocations[i] = path;
        }
        return null;
    }

    private string GetMasterPath(string[] locations, string viewName, 
                   string controllerName, string themeName, out string[] searchedLocations)
    {
        string path = null;

        searchedLocations = new string[locations.Length];

        for (int i = 0; i < locations.Length; i++)
        {
            path = string.Format(CultureInfo.InvariantCulture, locations[i], 
                                 new object[] { viewName, controllerName, themeName });
            if (this.VirtualPathProvider.FileExists(path))
            {
                searchedLocations = new string[0];
                return path;
            }
            searchedLocations[i] = path;
        }
        return null;
    }
}

To finish this sample, just change the global.asax, removing all engines from the View Engine and adding the custom one:

protected void Application_Start()
{
    RegisterRoutes(RouteTable.Routes);

    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new ThemeViewEngine());
}

Well, hope that’s useful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

TODODotNet
Portugal Portugal
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAreas in MVC 2memberYusree Allie21 Oct '10 - 1:27 
GeneralThank a lot!memberNguyen Tien Lam12 Sep '10 - 4:05 
GeneralIt's not workmemberNguyen Tien Lam12 Sep '10 - 1:42 
GeneralRe: It's not workmemberNguyen Tien Lam12 Sep '10 - 1:44 
GeneralSmall change needed to make it work in MVC2memberkeeeeeeeeeeiff11 Aug '10 - 5:50 
QuestionWhat to do if I have more than one Master page?memberprasadvemala31 Oct '09 - 1:44 
GeneralASP.NET MVC Dynamic Themes problem solvedmembertotallyweb8 May '09 - 15:40 
GeneralRe: ASP.NET MVC Dynamic Themes problem solvedmemberwex6923 Sep '09 - 3:58 
QuestionASP.NET MVC Dynamic Themesmembertotallyweb8 May '09 - 15:24 
GeneralMVC RC1memberbidulle3 Feb '09 - 0:24 
QuestionOr use .NET 'App_Themes' folder?memberAbishek Bellamkonda2 Feb '09 - 15:32 
AnswerRe: Or use .NET 'App_Themes' folder?memberdbau5679810 Apr '09 - 14:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 26 Jan 2009
Article Copyright 2009 by TODODotNet
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid