4

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

1

2 Answers 2

2

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;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

true - or check out the various overloads of the View method, two of them has the ability to specify which master page to render: msdn.microsoft.com/en-us/library/…
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 good point. It may make more sense to create a common base controller class and adding this code to OnActionExecuted, or overriding/overloading the .View() method
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.