2013-02-09 42 views
0

我正在使用ASPX引擎(不是RAZOR!)和Site.Master作爲母版頁的MVC-4項目。使用.aspx引擎的ASP.NET MVC DisplayModeProvider

現在我的任務是爲移動設備啓用此網站。在找到我所知道的所有可用信息後,我需要使用Razor執行以下任務:

  1. 在應用程序啓動中添加DisplayModeProvider。
  2. 創建_Layout.Phone.cshtml並添加jQuery移動支持。
  3. 創建支持手機的視圖,如Index.Phone.cshtml並使用_Layout.Phone.cshmtl作爲母版頁。

現在我的問題是如何啓用網站移動設備使用ASPX引擎。

我創建了Site.Phone.Master和Index.Phone.aspx,但它只渲染默認的Web頁面視圖而不是Phone視圖。

回答

2

你可以通過創建移動瀏覽器一個母版(~/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> 

,然後有AA移動視圖(~/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,......您只需調整委託人內部正在檢查用戶代理的情況。

+0

完美!這解決了我的問題。 – MaX 2013-02-10 01:50:02