asp.net實現識別用戶端瀏覽器或作業系統_實用技巧

來源:互聯網
上載者:User

1、通過 UserAgent

protected void Page_Load(object sender, EventArgs e)  {    //驗證終端    string agent = Request.Headers["User-Agent"];    if (choose_net(agent))    {      Response.Redirect("/mobile/");    }  }  //驗證終端  public bool choose_net(String userAgent)  {    if (userAgent.IndexOf("Noki") > -1 || // Nokia phones and emulators           userAgent.IndexOf("Eric") > -1 || // Ericsson WAP phones and emulators           userAgent.IndexOf("WapI") > -1 || // Ericsson WapIDE 2.0           userAgent.IndexOf("MC21") > -1 || // Ericsson MC218           userAgent.IndexOf("AUR") > -1 || // Ericsson R320           userAgent.IndexOf("R380") > -1 || // Ericsson R380           userAgent.IndexOf("UP.B") > -1 || // UP.Browser           userAgent.IndexOf("WinW") > -1 || // WinWAP browser           userAgent.IndexOf("UPG1") > -1 || // UP.SDK 4.0           userAgent.IndexOf("upsi") > -1 || //another kind of UP.Browser           userAgent.IndexOf("QWAP") > -1 || // unknown QWAPPER browser           userAgent.IndexOf("Jigs") > -1 || // unknown JigSaw browser           userAgent.IndexOf("Java") > -1 || // unknown Java based browser           userAgent.IndexOf("Alca") > -1 || // unknown Alcatel-BE3 browser (UP based)             userAgent.IndexOf("MITS") > -1 || // unknown Mitsubishi browser           userAgent.IndexOf("MOT-") > -1 || // unknown browser (UP based)           userAgent.IndexOf("My S") > -1 ||// unknown Ericsson devkit browser            userAgent.IndexOf("WAPJ") > -1 ||//Virtual WAPJAG www.wapjag.de           userAgent.IndexOf("fetc") > -1 ||//fetchpage.cgi Perl script from www.wapcab.de            userAgent.IndexOf("ALAV") > -1 || //yet another unknown UP based browser           userAgent.IndexOf("Wapa") > -1 || //another unknown browser (Web based "Wapalyzer")           userAgent.IndexOf("UCWEB") > -1 || //another unknown browser (Web based "Wapalyzer")           userAgent.IndexOf("BlackBerry") > -1 || //another unknown browser (Web based "Wapalyzer")                   userAgent.IndexOf("J2ME") > -1 || //another unknown browser (Web based "Wapalyzer")                userAgent.IndexOf("Oper") > -1 ||         userAgent.IndexOf("Android") > -1 ||      userAgent.IndexOf("mozilla") > -1)    {      return true;    }    else    {      return false;    }  }

2.考慮asp.net mvc

裡面有for mobile的模版

3.擷取瀏覽器的屬性

複製代碼 代碼如下:
HttpBrowserCapabilities bc=Request.Browser; Response.Write("<script language='javascript'>alert('" + bc.Browser + "');</script>");

 4.js判斷

<script type="text/javascript">    var bro = navigator.userAgent;    //alert(bro);    // alert(bro.indexOf("Windows Phone"));    // iPad 瀏覽器navigator.userAgent(包含iPad)    if (bro.indexOf("NetFront") > 0 || bro.indexOf("UCWEB") > 0      || bro.indexOf("iPhone") > 0 || bro.indexOf("Windows CE") > 0      || bro.indexOf("MIDP-2.0") > 0 || bro.indexOf("Android") > 0      || bro.indexOf("Opera Mini") > 0 || bro.indexOf("SymbianOS") > 0      || bro.indexOf("Windows Phone") > 0) {      window.open("Wap1/Index.aspx", "_top");    }    //    else if (bro.indexOf("Windows NT") > 0) {    //      window.open("NewWeb/default.html", "_top");    //    }        else {          window.open("NewWeb/default.html", "_top");        }  </script>

我們再來看下如何擷取PC端的用戶端資訊呢

/// <summary>/// 擷取作業系統的名字/// </summary>/// <param name="userAgent"></param>/// <returns></returns>private string GetOSNameByUserAgent(string userAgent){string osVersion = "未知"; if (userAgent.Contains("NT 10.0")){osVersion = "Windows 10";}else if (userAgent.Contains("NT 6.3")){osVersion = "Windows 8.1";}else if (userAgent.Contains("NT 6.2")){osVersion = "Windows 8";}else if (userAgent.Contains("NT 6.1")){osVersion = "Windows 7";}else if (userAgent.Contains("NT 6.1")){osVersion = "Windows 7";}else if (userAgent.Contains("NT 6.0")){osVersion = "Windows Vista/Server 2008";}else if (userAgent.Contains("NT 5.2")){if (userAgent.Contains("64"))osVersion = "Windows XP";elseosVersion = "Windows Server 2003";}else if (userAgent.Contains("NT 5.1")){osVersion = "Windows XP";}else if (userAgent.Contains("NT 5")){osVersion = "Windows 2000";}else if (userAgent.Contains("NT 4")){osVersion = "Windows NT4";}else if (userAgent.Contains("Me")){osVersion = "Windows Me";}else if (userAgent.Contains("98")){osVersion = "Windows 98";}else if (userAgent.Contains("95")){osVersion = "Windows 95";}else if (userAgent.Contains("Mac")){osVersion = "Mac";}else if (userAgent.Contains("Unix")){osVersion = "UNIX";}else if (userAgent.Contains("Linux")){osVersion = "Linux";}else if (userAgent.Contains("SunOS")){osVersion = "SunOS";}else{osVersion = HttpContext.Current.Request.Browser.Platform;}return osVersion;}

基本上很全了,小夥伴們根據需求自由選擇吧

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.