【问题标题】:ASP.NET MVC DisplayModeProvider with .aspx engine带有 .aspx 引擎的 ASP.NET MVC DisplayModeProvider
【发布时间】:2013-02-10 04:42:06
【问题描述】:

我正在使用 ASPX 引擎(不是 RAZOR!)和 Site.Master 作为母版页的 MVC-4 项目。

现在我的任务是为移动设备启用此站点。找到我知道的所有可用信息后,我需要使用 Razor 完成以下任务

  1. 在应用程序启动中添加 DisplayModeProvider。
  2. 创建 _Layout.Phone.cshtml 并添加 jQuery 移动支持。
  3. 创建手机支持的视图,例如 Index.Phone.cshtml,并使用 _Layout.Phone.cshmtl 作为母版页。

现在我的问题是如何使用 ASPX 引擎为移动设备启用网站。

我创建了 Site.Phone.Master 和 Index.Phone.aspx,但它只呈现默认网页视图而不是电话视图。

【问题讨论】:

    标签: asp.net-mvc mobile


    【解决方案1】:

    您可以从为移动浏览器创建 MasterPage (~/Views/Shared/Site.Mobile.Master) 开始:

    <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
    <!DOCTYPE html>
    <html lang="en">
    <head runat="server">
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        This is the mobile master page
    
        <asp:ContentPlaceHolder ID="MainContent" runat="server" />
    </body>
    </html>
    

    然后有一个将使用此母版页的移动视图 (~/Views/Home/Index.Mobile.aspx):

    <%@ Page 
        Language="C#" 
        MasterPageFile="~/Views/Shared/Site.Mobile.Master" 
        Inherits="System.Web.Mvc.ViewPage" 
    %>
    
    <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
        This is the mobile view
    </asp:Content>
    

    好的,现在剩下的就是在你的Application_Start 中插入显示模式:

    Func<HttpContextBase, bool> contextCheckDelegate = ctx =>
    {
        // Here you could use the ctx variable which represents the HttpContextBase
        // in order to test the UserAgent of the Request and decide whether it is coming
        // from a mobile device or not and return true or false respectively which is what
        // will determine if your .Mobile masterpage and view will be used for this request
        if (IsMobile)
        {
            return true;
        }         
        return false;
    };
    DefaultDisplayMode mobileMode = new DefaultDisplayMode("Mobile");
    mobileMode.ContextCondition = contextCheckDelegate;
    DisplayModeProvider.Instance.Modes.Insert(0, mobileMode);
    

    您当然可以拥有更具体的移动视图,例如 iPhone、iPad 等,您只需调整委托内部使用的条件,即检查用户代理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多