Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically changing Master Template in ASP.NET MVC

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.
like image 572
pgb Avatar asked Nov 07 '08 14:11

pgb


1 Answers

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
like image 166
Slee Avatar answered Oct 03 '22 20:10

Slee