5

I have the requirement to support different Master pages on my application (ASP.NET MVC). What is the recommended way to:

  1. Pass the master page name to the view from.
  2. Store the master page (in session, or something) so it sticks during a user's visit.

3 Answers 3

9

Use a custom base controller and inherit from it instead:

Public Class CustomBaseController
    Inherits System.Web.Mvc.Controller

    Protected Overrides Function View(ByVal viewName As String, ByVal masterName As String, ByVal model As Object) As System.Web.Mvc.ViewResult

       Return MyBase.View(viewName, Session("MasterPage"), model)

    End Function

End Class

I set my Session variable in the global.asax Session_Start:

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)

//programming to figure out your session
Session("MasterPage")="MyMasterPage"

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

This is an excellent answer, just to update it slightly in MVC3 you can also now create custom Razor View Engine which may be cleaner: weblogs.asp.net/imranbaloch/archive/2011/06/27/…
0

you could throw the master page name into the session, but sessions are unreliable. i'd recommend throwing it in a db instead.

once you're in the page, you can change/set the master page by accessing page.masterpagefile. it's a string; just pass the .master name in.

Comments

-2

Why not keep the Master Page on the user profile? Then just change it on the PreLoad event.

http://www.odetocode.com/articles/440.aspx

3 Comments

I'm using ASP.NET MVC. Shouldn't the controller decide what page to use?
yep. Probably you should use a base controller.
The question specifically mentions that it's for ASP.NET MVC and not ASP.NET

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.