views:

258

answers:

2

I'm attempting to create themeing system for my asp.net mvc application.

I was able to create a custom view engine and a custom controller class to change the way views are output.

My issue is masterpages. Is there a way to do this following: This is the @Page directive of the view

<%@ Page Title="" Language="C#" 
MasterPageFile="<%= ViewData["myThemeFolder"] + "/Views/Shared/Site.Master"  %>" 
Inherits="System.Web.Mvc.ViewPage" %>

I tried to do that, but it doesn't work. I would like to accomplish that, but don't know how.

A: 

What you need to do is set that MasterPageFile property to a "base" master that has the "standard" set of Content controls on it.

Then dynamically select the master from your controller:

return View(viewName, masterName, modelObject);

You can't handle this in the View because that part of the View doesn't have any clue what ViewData is, etc. It is pretty much a preprocessor directive.

Anyhow, what you can do is one of the following:

1) Add a ThemedView (or whatever you wish to call it) method in your base Controller implementation, then return that rather than View() to return your ViewResults.

2) Create a custom ActionFilterAttribute to dynamically swap the master page.

Either way you will either have to use your new method or decorate things with the ActionFilterAttribute.

Wyatt Barnett
I'm not really sure what you mean. I don't want to have to explicity type the mastername for every view I generate. My base controller class sets ViewData["myThemeFolder"] so I would like it to automatically set the masterpage
Baddie
That would be the "more elegant" way to handle it. Like creating a ThemedView method in the base class that punches in the master page name.
Wyatt Barnett
If I took this approach, I would need to it for about 50-60 actions, not very feasible for what I need. If I was able to use view data in the @Page directive, all would be well.
Baddie
A: 

I ended up doing theming using stylesheet replacement. I'm using jQuery UI and associated themes from ThemeRoller. You might want to think about doing this or something similar as an alternative to changing the master page. You can find more info on how I did this on my blog, where I've written about a jQuery plugin I use to allow people to change themes by essentially picking a different theme stylesheet.

tvanfosson
I would like to have more control over the template rather than style it completely with css.
Baddie