技术开发 频道

使用ASP.NET MVC检测、重定向移动设备

  步骤3:设置web.config

  为了利用API,需要为web.config文件添加下面的内容。

  (1)Configuration部分:

  以下设置需要放置在web.config文件的顶部。它们通知.NET在web.config中后续配置的有关信息,以及如何处理它们。在本例中,我们告诉.NET要使用Mobile程序集。

  Web.config Setting1:
<configSections>
  
<sectionGroup name="mobile">
      
<section name="toolkit" type="Mobile.Configuration.ToolkitSection, Mobile,
                  Version=0.1.5.0, Culture=neutral"
allowDefinition="Everywhere"
                  restartOnExternalChanges
="false" allowExeDefinition="MachineToApplication"/>
      
<section name="wurfl" type="Mobile.Devices.Wurfl.Configuration.WurflSection, Mobile,
                   Version=0.1.5.0, Culture=neutral"
allowDefinition="Everywhere"
                   restartOnExternalChanges
="false" allowExeDefinition="MachineToApplication"/>
  
</sectionGroup>
</configSections>

   注意:版本号0.1.5.0应该改成你使用的Mobile.dll的版本号。其它部分无需修改。

  (2)添加新的mobile部分:

  在configSections元素之后添加如下所示的mobile元素。这些代码管理Mobile API对移动设备的响应方式,以及从哪里寻找移动设备的数据库。

Web.config Setting2:
<mobile>
  
  
<!-- When a mobile device is found to be accessing a non mobile page the
      mobileRedirectUrl setting is used to redirect the browser to a landing
      page for mobile devices.
-->
  
<toolkit mobileRedirectUrl="~/M.aspx" logFile="~/App_Data/Log.txt" logLevel="Info"/>
  
  
<!-- The following settings provided the location of wurfl files. wurflFilePath
      is the path of the main wurfl file (mandatory). newDevicesPatchFile shows where
      devices that aren't matched exactly should be stored (optional). wurflPatches
      defines where any additional patch files can be located (optional).
-->
  
<wurfl wurflFilePath="~/App_Data/wurfl.xml.gz">
      
<wurflPatches>
        
<add name="browser_definitions"
                 filePath
="~/App_Data/web_browsers_patch.xml.gz"
                 enabled
="true"/>
      
</wurflPatches>
  
</wurfl>
</mobile>

    (3)Detector模块:

  将下面的元素添加到httpModules元素中,这允许Mobile API拦截新的页面请求,并且如果请求时移动设备发出的话,就重定向它们。

Web.config Setting3:

<httpModules>
  
<!-- Registers a module that is used to detect any new
      requests to the web site. Without this module mobile detection and
      redirection won't work.
-->
  
<add name="Detector" type="Mobile.Browser.Detector, Mobile, Version=0.1.5.0"/>
</httpModules>

   注意:版本号0.1.5.0应该改成你使用的Mobile.dll的版本号。其它部分无需修改。

  步骤4:移动页面(M.aspx)

  将以下代码添加到M.aspx和M.aspx.cs中:

  M.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="M.aspx.cs" Inherits="M" %>
<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>

<html xmlns="http://www.w3.org/1999/xhtml"
<body
>
  
<mobile:Form id="Form1" runat="server">
      
<mobile:Label ID="LabelMobile" Runat="server" Alignment="Center" Font-Size="Large"
                    Text
="This is a Mobile Device Redirection." />
      
<mobile:Label ID="LabelManufacturer" Runat="server" Alignment="Center" Font-Size="Normal"/>
      
<mobile:Label ID="LabelModel" Runat="server" Alignment="Center" Font-Size="Normal"/>
      
<mobile:Label ID="LabelScreen" Runat="server" Alignment="Center" Font-Size="Normal"/>
      
<mobile:Label ID="LabelPlatform" Runat="server" Alignment="Center" Font-Size="Normal"/>
      
<mobile:Label ID="LabelBrowser" Runat="server" Alignment="Center" Font-Size="Normal"/>
      
<mobile:Label ID="LabelJpg" Runat="server" Alignment="Center" Font-Size="Normal"/>
      
<mobile:Label ID="LabelPng" Runat="server" Alignment="Center" Font-Size="Normal"/>
      
<mobile:Label ID="LabelGif" Runat="server" Alignment="Center" Font-Size="Normal"/>
  
</mobile:Form>
</body>
</html>

   M.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Mobile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class M : System.Web.UI.MobileControls.MobilePage
{
  
protected void Page_Load(object sender, EventArgs e)
  {
    
string strUserAgent = Request.UserAgent;
     LabelManufacturer.Text
= "Manufacturer : " + Request.Browser.MobileDeviceManufacturer;
     LabelModel.Text
= "Model : " + Request.Browser.MobileDeviceModel;
     LabelScreen.Text
= "Screen : " + Request.Browser.ScreenPixelsWidth.ToString() +
                        
" x " + Request.Browser.ScreenPixelsHeight.ToString();

    
//Apart from standard capability information provided by "Request.Browser object",
    
//.NETMobileAPI provides more detailed information about the device capabilities.
     LabelPlatform.Text = "Platform : " + Mobile.Devices.MobileDevices.GetCapability(strUserAgent, "device_os");
     LabelBrowser.Text
= "Browser : " + Mobile.Devices.MobileDevices.GetCapability(strUserAgent, "mobile_browser");
     LabelJpg.Text
= "Jpg Image : " + Mobile.Devices.MobileDevices.GetCapability(strUserAgent, "jpg");
     LabelPng.Text
= "Png Image : " + Mobile.Devices.MobileDevices.GetCapability(strUserAgent, "png");
     LabelGif.Text
= "Gif Image : " + Mobile.Devices.MobileDevices.GetCapability(strUserAgent, "gif");

    
//Note: For more capabilities please refer to App_Data/Capabilities.xml file.
  }
}
0
相关文章