Dynamically changing master template in ASP.NET MVC

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

  • Pass the name of the homepage to the view from.
  • Save the main page (in a session or something else) so that it freezes during a user visit.
+5
source share
3 answers

Use your own base controller and inherit 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 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
+9
source

you could transfer the name of the main page to the session, but the sessions are unreliable. I would recommend throwing it in db instead.

, / , page.masterpagefile. ; .master .

0

Why not save the homepage in your user profile? Then just change it to the PreLoad event.

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

-2
source

All Articles