asp.net实现识别客户端浏览器或操作系统
1、通过UserAgent
protectedvoidPage_Load(objectsender,EventArgse) { //验证终端 stringagent=Request.Headers["User-Agent"]; if(choose_net(agent)) { Response.Redirect("/mobile/"); } } //验证终端 publicboolchoose_net(StringuserAgent) { if(userAgent.IndexOf("Noki")>-1||//Nokiaphonesandemulators userAgent.IndexOf("Eric")>-1||//EricssonWAPphonesandemulators userAgent.IndexOf("WapI")>-1||//EricssonWapIDE2.0 userAgent.IndexOf("MC21")>-1||//EricssonMC218 userAgent.IndexOf("AUR")>-1||//EricssonR320 userAgent.IndexOf("R380")>-1||//EricssonR380 userAgent.IndexOf("UP.B")>-1||//UP.Browser userAgent.IndexOf("WinW")>-1||//WinWAPbrowser userAgent.IndexOf("UPG1")>-1||//UP.SDK4.0 userAgent.IndexOf("upsi")>-1||//anotherkindofUP.Browser userAgent.IndexOf("QWAP")>-1||//unknownQWAPPERbrowser userAgent.IndexOf("Jigs")>-1||//unknownJigSawbrowser userAgent.IndexOf("Java")>-1||//unknownJavabasedbrowser userAgent.IndexOf("Alca")>-1||//unknownAlcatel-BE3browser(UPbased) userAgent.IndexOf("MITS")>-1||//unknownMitsubishibrowser userAgent.IndexOf("MOT-")>-1||//unknownbrowser(UPbased) userAgent.IndexOf("MyS")>-1||//unknownEricssondevkitbrowser userAgent.IndexOf("WAPJ")>-1||//VirtualWAPJAGwww.wapjag.de userAgent.IndexOf("fetc")>-1||//fetchpage.cgiPerlscriptfromwww.wapcab.de userAgent.IndexOf("ALAV")>-1||//yetanotherunknownUPbasedbrowser userAgent.IndexOf("Wapa")>-1||//anotherunknownbrowser(Webbased "Wapalyzer") userAgent.IndexOf("UCWEB")>-1||//anotherunknownbrowser(Webbased "Wapalyzer") userAgent.IndexOf("BlackBerry")>-1||//anotherunknownbrowser(Web based"Wapalyzer") userAgent.IndexOf("J2ME")>-1||//anotherunknownbrowser(Webbased "Wapalyzer") userAgent.IndexOf("Oper")>-1|| userAgent.IndexOf("Android")>-1|| userAgent.IndexOf("mozilla")>-1) { returntrue; } else { returnfalse; } }
2.考虑asp.netmvc
里面有formobile的模版
3.获取浏览器的属性
HttpBrowserCapabilitiesbc=Request.Browser;Response.Write("<scriptlanguage='javascript'>alert('"+bc.Browser+"');</script>");
4.js判断
<scripttype="text/javascript"> varbro=navigator.userAgent; //alert(bro); //alert(bro.indexOf("WindowsPhone")); //iPad浏览器navigator.userAgent(包含iPad) if(bro.indexOf("NetFront")>0||bro.indexOf("UCWEB")>0 ||bro.indexOf("iPhone")>0||bro.indexOf("WindowsCE")>0 ||bro.indexOf("MIDP-2.0")>0||bro.indexOf("Android")>0 ||bro.indexOf("OperaMini")>0||bro.indexOf("SymbianOS")>0 ||bro.indexOf("WindowsPhone")>0){ window.open("Wap1/Index.aspx","_top"); } //elseif(bro.indexOf("WindowsNT")>0){ //window.open("NewWeb/default.html","_top"); //} else{ window.open("NewWeb/default.html","_top"); } </script>
我们再来看下如何获取PC端的客户端信息呢
///<summary> ///获取操作系统的名字 ///</summary> ///<paramname="userAgent"></param> ///<returns></returns> privatestringGetOSNameByUserAgent(stringuserAgent) { stringosVersion="未知"; if(userAgent.Contains("NT10.0")) { osVersion="Windows10"; } elseif(userAgent.Contains("NT6.3")) { osVersion="Windows8.1"; } elseif(userAgent.Contains("NT6.2")) { osVersion="Windows8"; } elseif(userAgent.Contains("NT6.1")) { osVersion="Windows7"; } elseif(userAgent.Contains("NT6.1")) { osVersion="Windows7"; } elseif(userAgent.Contains("NT6.0")) { osVersion="WindowsVista/Server2008"; } elseif(userAgent.Contains("NT5.2")) { if(userAgent.Contains("64")) osVersion="WindowsXP"; else osVersion="WindowsServer2003"; } elseif(userAgent.Contains("NT5.1")) { osVersion="WindowsXP"; } elseif(userAgent.Contains("NT5")) { osVersion="Windows2000"; } elseif(userAgent.Contains("NT4")) { osVersion="WindowsNT4"; } elseif(userAgent.Contains("Me")) { osVersion="WindowsMe"; } elseif(userAgent.Contains("98")) { osVersion="Windows98"; } elseif(userAgent.Contains("95")) { osVersion="Windows95"; } elseif(userAgent.Contains("Mac")) { osVersion="Mac"; } elseif(userAgent.Contains("Unix")) { osVersion="UNIX"; } elseif(userAgent.Contains("Linux")) { osVersion="Linux"; } elseif(userAgent.Contains("SunOS")) { osVersion="SunOS"; } else { osVersion=HttpContext.Current.Request.Browser.Platform; } returnosVersion; }
基本上很全了,小伙伴们根据需求自由选择吧