Dynamically changing Master Template in ASP.NET MVC

前端 未结 3 1641
执笔经年
执笔经年 2020-12-30 16:12

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
3条回答
  •  长发绾君心
    2020-12-30 16:41

    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
    

提交回复
热议问题