asp.net:如何检测iOS/Android

本文关键字:检测 iOS Android net 何检测 asp | 更新日期: 2023-09-27 17:58:14

我最近推出了一个用C#/.net 4.0编写的web应用程序,广泛使用了jQuery&jQuery UI,以提供尽可能好的用户体验。

然而,一些用户在通过iPhone或Android设备使用该网站时报告了问题。

检测iOS和Android的最佳方法是什么这样我就可以调整每个浏览器的UI了。

asp.net:如何检测iOS/Android

一般来说,您应该检测浏览器服务器端,然后将正确的模板/视口渲染到浏览器。检查浏览器发送到服务器的用户代理字符串-iOS通常包含子字符串,"iPhone"和Android使用"Android",然后是版本号("Android 2.2")

我认为最好的解决方案是尝试字符串

Request.ServerVariables["HTTP_USER_AGENT"]

到目前为止,这将为android设备返回android一词。Windows很容易检测。有人帮我拿另一种水果。

有关详细解决方案,请访问http://www.codeproject.com/Articles/34422/Detecting-a-mobile-browser-in-ASP-NET

这里有一个ASP.NET扩展(显然,我还没有使用它)为您做所有这些:)

http://51degrees.codeplex.com/

IsMobileDevice在我的Android手机上没有给出正确的响应。这是我用过的代码。

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

protected void  TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void  Button1_Click(object sender, EventArgs e)
{
    System.Web.HttpBrowserCapabilities browser = Request.Browser;
    string s = "Browser Capabilities'n"
        + "Type = " + browser.Type + "'n"
        + "Name = " + browser.Browser + "'n"
        + "Version = " + browser.Version + "'n"
        + "Major Version = " + browser.MajorVersion + "'n"
        + "Minor Version = " + browser.MinorVersion + "'n"
        + "Platform = " + browser.Platform + "'n"
        + " Is MobileDevice ? " + browser.IsMobileDevice + "'n"
        + " screen Height " + browser.ScreenCharactersHeight + "'n"
        + " screen width " + browser.ScreenCharactersWidth + "'n"
        + "Is Beta = " + browser.Beta + "'n"
        + "Is Crawler = " + browser.Crawler + "'n"
        + "Is AOL = " + browser.AOL + "'n"
        + "Is Win16 = " + browser.Win16 + "'n"
        + "Is Win32 = " + browser.Win32 + "'n"
        + "Supports Frames = " + browser.Frames + "'n"
        + "Supports Tables = " + browser.Tables + "'n"
        + "Supports Cookies = " + browser.Cookies + "'n"
        + "Supports VBScript = " + browser.VBScript + "'n"
        + "Supports JavaScript = " +
            browser.EcmaScriptVersion.ToString() + "'n"
        + "Supports Java Applets = " + browser.JavaApplets + "'n"
        + "Supports ActiveX Controls = " + browser.ActiveXControls
              + "'n"
        + "Supports JavaScript Version = " +
            browser["JavaScriptVersion"] + "'n";
    TextBox1.Text = s;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged" 
            Width="955px" Rows="22" TextMode="MultiLine"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="Find browser" Font-Size="Large" Width="145px" />
    </div>
    </form>
</body>
</html>

你可以在尝试代码www.abcd.com.au/browser.aspx使用您的手机或其他设备。