Project Description
This is a library with a demo project for ASP.NET MVC3 websites using the Razor Engine although it works as well for the Web Forms engine.

The library builds upon work by others by fixing flaws and enhancing it with new functionality. It allows the developer to seamlessly integrate application themes in their MVC3 web applications as well as localization of views and partial views with a minimum amount of work.

If you are into MVC you probably know that if you want application themes you will have to cook your own unlike the Web Forms engine.

With this project you leverage on your knowledge of Web Forms to use the same theme configuration setup with minor variations to fully and seamlessly implement Themes and Localization in your web application.

The default Visual Studio website projects are bare bones even though the MVC template already includes (cheers!) ASP.NET membership in its basic form.

However, ASP.NET websites with the Web Forms engine has built-in support for themes, the MVC website template with the Razor engine does not have built-in support for themes.

Likewise both view engines (web forms & Razor) have support for localization although it is more intuitive in the web forms engine.

The Problem

You develop a new website and as it turns out your are also required that it serves localized content, i.e. pages in other supported languages.

Additionally you start with a single look but at some point you also want to be able to change the look of the website without a major redesign, preferably just by changing a setting once the necessary layouts and/or stylesheets have been designed.

The Challenge

MVC doesn't really support a built-in theme as web-forms. You can use jQuery themes but that is as far as it goes. There is no automatic theme detection.

I also needed to later on add localization without having to do anything major.

So, my requirements where:
  • Be able to switch the theme of the website with a web.config setting
  • Have a theme engine that operated similar to the web.forms themes
  • That a theme could be more than just having a different color palette,
it should also be able to define a different page layout for a theme.
  • That page (views) localizations in MVC be as transparent as possible without a need to multiplicate the view, just the content.
  • I don't want to redesign the wheel but build upon existent solutions

So that is what this solution does for you. It allows you to define several themes (you must develop the theme though) some with a general layout, others with their own layout. It also makes it possible to localize the views in an intuitive way and have the same view engine find it and render it without any wizardry of your own.

Credit where it is due

As I mentioned in the requirements, I was in no mood to redesign the wheel and did not want to fall into the "Not made here" pitfall, I like to use my time effectively but give credit where it is due.

So, this Code Plex project is an improvement (and fixes) upon a couple of implementations that were already out there and that were either broken, outdated or operational but not compliant to my needs. So, my task was to merge them, fix what was broken and improve upon them.

The source of this project was originally taken from what was described in Thang Chung's Context is King blog post titled "Razor Themed View Engine for multi-themes site". That source code was for an older ASP.NET MVC version and no longer compiled. I fixed it and also addressed the flaws of that design.

However, by reading that post and others it turned out his version was based a lot on the ASP.NET MVC Theme supported razor view engine post on Kazi Manzur Rashid's "Sharing thoughts and learning" blog.

Thang Chung's source was also originally based on modifications made by him on previous work by Chris Pietschmann blog post title ASP.NET MVC implement theme folders using a custom view engine made in 2009. It was also based on work done by Christophe Geers blog post ASP.NET MVC Themed View Engine.

Further examination revealed that the (old) solutions either didn't compile or had several flaws that seriously affected maintainability. For example one solution required you to make multiple copies of the whole view to provide content with a different theme, that simply defeated the purpose. It would be an even greater nightmare when localization was added (as I required).

So thanks to the previous work by these gentlemen and some rethinking and development on my part I modified Thang Chung's source project and made it what it is now, something functional that offers both flexible theming and localization (which was not covered by their solutions). These changes are described in the Initial Change Log of the first release.

Last edited Jun 13, 2012 at 8:00 PM by lordofscripts, version 3