views:

1482

answers:

3

I don't know how to using dynamic master page in ASP.NET MVC RC 1.0. Please help!

+2  A: 

There's a reasonably maintainable solution discussed on http://forums.asp.net/p/1394235/2991293.aspx where you create a common site master descendant and use that for all your own views. There's also a CodeProject entry that looks interesting that uses a custom ViewEngine at http://www.codeproject.com/KB/aspnet/ASPNETMVCDynamicThemes.aspx.

I need to do this too, but I haven't tried either solution yet so I don't have a feel yet for which is better overall. IMO, dynamic skinning is a crucial feature for MVC, particularly to cater to user personalization.

John Kaster
Isn't Dynamic skinning called 'CSS'? :-D
George Stocker
George, CSS would be part of it, but personalized or customized views could definitely exceed what CSS can offer.
John Kaster
+8  A: 

You can specify the name of the master page when using the View() helper method:

return View("About", "AlternateMaster", model);

AlternateMaster would resolve to ~/Views/Shared/AlternateMaster.master

Found this here

John Sheehan
+2  A: 

I got this to work by creating a base controller that handled the OnActionExecuted event. In the OnActionExecutedevent I assign the master page. Then I made all my other controllers inherit from the base class.

public class BaseController : Controller
{
     protected override void OnActionExecuted(ActionExecutedContext filterContext) {
         var action = filterContext.Result as ViewResult;
         if (action != null) {
             action.MasterName = MyApp.Properties.Settings.Default.Theme;
         }  

         base.OnActionExecuted(filterContext);
     }
}

I wrote a post about this if you want more detail

Michael Ceranski