tags:

views:

40

answers:

2

how to dynamically change masterpage in asp.net mvc application. Like in ASP.Net it can be changed in Page_PreInit Event

+1  A: 

Views expose the .MasterName property which specifies which master page to use. You can set this in your controller when returning a view.

For example,

    public ActionResult Index()
    {
        ViewResult vr = View();
        vr.MasterName="....";
        return vr;
    }
David Lively
true - or check out the various overloads of the `View` method, two of them has the ability to specify which master page to render: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller_methods(v=VS.100).aspx
mookid8000
If it's something you want to apply more generally and site wide, this may be way too laborious to have to apply this in each Action for a bunch of Views.
Wim Hollebrandse
+1  A: 

You can create your own custom ViewPage class, and override the OnPreInit method and set the MasterPageFile property accordingly.

Just change your Views to use your own custom ViewPage class, and you're done.

Wim Hollebrandse