Dynamically changing Master Template in ASP.NET MVC

拜拜、爱过 提交于 2019-11-30 07:35:47

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

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.

Bruno Shine

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!