Reputation: 1201
I have the requirement to support themeing of my site's pages. The way I am doing this is by dynamically choosing a master page based on the current theme.
I have setup a directory structure like so
/shared/masterpages/theme1/Master1.master /shared/masterpages/theme1/Master2.master /shared/masterpages/theme1/Master3.master
/shared/masterpages/theme2/Master1.master /shared/masterpages/theme2/Master2.master /shared/masterpages/theme2/Master3.master
And I am still using the page directive in the view
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/shared/masterpages/theme1/Master1.Master"%>
I would still like to leverage the view's MasterPageFile property and just change the theme directory.
I can only think of three ways to do this none of them which sound great.
Option 1 seems the best option to me so far. Does anyone else have any thoughts on how to do this?
Upvotes: 0
Views: 263
Reputation: 60624
Updated suggestion:
Since my original suggestion didn't work out as I had expected, here's a possible way to work around it, while still keeping your action methods clean, and minimizing repetition of code:
ActionResult
that adds the master name/theme name/whatever info you need to pick the correct master page into ViewData["masterInfo"]
(or something similar).System.Web.Mvc.ViewPage
. If you need, also create a generic version that inherits from .ViewPage<T>
.ViewData["masterInfo"]
. I'm not sure if there's a need or not, but don't forget to run the base constructor, either before or after your code, if there is one that needs to run.System.Web.Mvc.ViewPage
.Original post:
Why not have an ActionFilter
, that can be applied on controller level, that sets the MasterPageFile
property of the view? If you override OnActionExecuted
, it shouldn't be too tricky to test if the result was a ViewResult
and in that case change the property to the correct value.
Upvotes: 2