Background

This project started out when we needed a themes framework for the http://mvcforum.codeplex.com project. The way it's made, there's no reason why you couldn't use it in any other ASP.NET MVC project.

The setup

We're using Ninject and the Ninject MVC extension in the project, but there's no reason why you couldn't use any other dependency injection framework, as long as the DependencyResolver can resolve IThemeProvider and IThemeURLProvider.

The inner workings

Take a look at our Code Sample.

To use MVC Themes in your ASP.NET MVC application, add a reference to the MVCThemes.dll assembly.

The controllers (or actions) that should use themes must be decorate with the Themed attribute.

The Themed attribute is an ActionFilter attribute and whenever a controller (or action) is executing, the Themed attribute will (using the DependencyResolver to get the IThemeProvider) find out which theme is selected. In the sample web-site, the QuerystringThemeProvider action filter will look for 'theme' in the querystring.

With the theme determined, ASP.NET MVC takes over. The custom implemented view engine (with the regular Razor Engine as base) will now take over. With the IThemeURLProvider (again using the DependencyResolver) the view engine will add the theme paths to the usual locations to try and find a matching view or partial view.

Doing your own thing

By implementing your own classes with the IThemeProvider and IThemeURLProvider interfaces, you can use whatever rules you want to determine the theme, and build the path where the view engine will search for a matching view or partial view.

You can take a look at the ForumThemeProvider class in the http://mvcforum.codeplex.com project for another example of a IThemeProvider implementation.

Last edited Aug 3, 2012 at 9:40 AM by steentottrup, version 4

Comments

serdarguner Sep 14, 2014 at 10:28 PM 
Reading Theme from web.config appsettings tag
public class ConfigThemeProvider : IThemeProvider
{
private const String themeKey = "mcvConfigTheme";

public void SetTheme(ActionExecutingContext filterContext)
{
String theme = System.Configuration.ConfigurationManager.AppSettings["Thema"].ToString();
if (!String.IsNullOrWhiteSpace(theme))
{
filterContext.HttpContext.Items.Add(ConfigThemeProvider.themeKey, theme);
}
}

private String GetTheme(IDictionary items)
{
Object theme = items[ConfigThemeProvider.themeKey];
if (theme != null && !String.IsNullOrWhiteSpace((String)theme))
{
return (String)theme;
}

return String.Empty;
}

public String GetTheme(ControllerContext controllerContext)
{
return this.GetTheme(controllerContext.HttpContext.Items);
}

public String GetTheme()
{
return this.GetTheme(HttpContext.Current.Items);
}
}

devdavko Feb 13, 2013 at 6:34 AM 
Hello, thanks for sharing the application. I dont want to include the theme name in the query string. Where is the suggested place to store / set the theme?

steentottrup Nov 10, 2011 at 7:44 AM 
There's no "selecting which theme" in the sample site. In the sample site, the theme is selected by putting a value in the querystring. If you want to let an administator or the visiting user to select a theme, you'll have to implement that yourself. take a look at http://mvcforum.codeplex.com/SourceControl/changeset/view/795417feaa27#mvcForum.Web%2fForumThemeProvider.cs to see how you can use the MVC Themes project with your own code. You'll have to write the code yourself that stores and retrieves the theme from a database (or where ever you want to store it). I hope this helps, if not, please contact me again!

piglet1 Nov 9, 2011 at 1:03 AM 
I have downloaded the sample site, searched the source code for the code of selecting which theme to run but can't find!