扩展的浏览器功能

更新:2007 年 11 月

MobileCapabilities 类是在 ASP.NET 的标准浏览器功能的基础上建立的。在客户端连接到 ASP.NET 移动网页时,ASP.NET 根据请求中的信息确定发出请求的设备,然后将 MobileCapabilities 对象附加到该请求。应用程序可以通过 HttpRequest 对象的 Browser 属性(该属性映射到 Page 对象的 Request 属性)访问此对象。

应用程序代码可以用两种方式访问 MobileCapabilities 对象中的各个功能。首先,它可以访问任何高级功能属性。每一只读属性都返回相应功能的类型安全值或者相应的默认值。下面的示例说明如何使用高级属性。

if (((MobileCapabilities)Request.Browser).ScreenCharactersWidth > 20)
{
    // Coding for big screen capabilities is placed here.
}
else
{
    // Coding for small screen capabilities is placed here.
}

其次,应用程序代码可以通过使用默认索引器,将功能作为字典访问。字典值与 Machine.config 或 Web.config 文件的 <browserCaps> 节中的字典值相同。返回值始终为 string 或 null 引用(如果没有设置该值)。如果需要,应用程序必须分析此字符串。下面的示例演示与前例同样的功能,但使用字典属性。

String screenWidthText = Request.Browser["screenCharactersWidth"];
int screenWidth = 40;
if (screenWidthText != null)
    screenWidth = Int32.Parse(screenWidthText);
if (screenWidth > 20)
{
    // This block contains code supporting a larger screen size.
}
else
{
    // This block contains code supporting a smaller screen size.
}
说明:

从字典项派生的 MobileCapabilities 属性(而不是在 Web.config 文件中直接定义的属性)可能在某些设备中导致不匹配。

请参见

参考

MobileCapabilities

其他资源

移动设备功能