开发者

ASP.NET MVC BaseController to dynamically set MasterPage file

开发者 https://www.devze.com 2023-01-02 16:36 出处:网络
I\'ve buil开发者_StackOverflow社区t a Base Controller that all of my Controllers inherit from, and I\'ve got it setup so that it checks the browser type and returns the appropriate MasterPageFile on t

I've buil开发者_StackOverflow社区t a Base Controller that all of my Controllers inherit from, and I've got it setup so that it checks the browser type and returns the appropriate MasterPageFile on the fly.

I'm wondering if this is an efficient way to do this or if I should optimize it another way.

Public Class BaseController : 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

        If Request.Browser.IsMobileDevice Then
            Return MyBase.View(viewName, "Mobile", model)
        Else
            Return MyBase.View(viewName, "Site", model)
        End If

    End Function

End Class

Also, if anyone is interested, I am using the information found here to enhance my Request.Browser.IsMobileDevice checks.

The .browser file I'm using can be found here.


IMHO this is a very good approach: based on request parameters (or more specifically HTTP headers in this case) the controller decides which view to render.


You may need to explore doing this in the ViewPage, ie create a BaseViewPage rather than doing it in the Controller?

Added: This might help:

How to use dynamic master page in ASP.NET MVC RC 1.0

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号