2013-02-09 43 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