[英]What is the best way to check the user's browser in ASP.NET?
What is the best way to check if the user is using a mobile browser? 检查用户是否正在使用移动浏览器的最佳方法是什么? I have seen some code, is the best way really thousands of if(...|| ...||) statements or is there a good api, if so which api does the job best(and for free preferably).
我看过一些代码,这是成千上万条if(... || ... ||)语句的最佳方式,或者是否有一个好的api,如果这样的话,api表现最好(最好是免费的)。 Thanks in advance.
提前致谢。
ps Am using c#, asp.net 3.5. ps我使用c#,asp.net 3.5。
The best thing to do it actually not detect the browser. 最好的做法实际上是不会检测到浏览器。 The number of browsers available, along with their various versions (and the likelihood of more coming along) means it isn't really practical to do.
可用的浏览器数量以及它们的各种版本(以及随之而来的更多版本)意味着这样做并不实际。 Even the number of devices has rocketed now that there are phones, tablets, netbooks and computers of all sizes.
现在有各种尺寸的电话,平板电脑,上网本和计算机,甚至设备的数量也猛增。
So instead of detecting a browser, grab the resolution of the device and use that... ie "This device is small, medium or large". 因此,不要检测浏览器,而是获取设备的分辨率并使用该分辨率……即“此设备是小型,中型或大型”。
If you need this information for document style, you can use device stylesheets. 如果您需要有关文档样式的信息,则可以使用设备样式表。
<link rel="stylesheet" href="standard.css" media="screen" type="text/css" />
<link rel="stylesheet" href="mobile.css" media="handheld" type="text/css">
How about using a browser file 如何使用浏览器文件
http://www.codeproject.com/KB/aspnet/browsercaps.aspx http://www.codeproject.com/KB/aspnet/browsercaps.aspx
And some example ones 还有一些例子
http://mdbf.codeplex.com/ http://mdbf.codeplex.com/
http://slingfive.com/pages/code/browserCaps/ http://slingfive.com/pages/code/browserCaps/
http://browsers.garykeith.com/ http://browsers.garykeith.com/
More browsercap files can be found here: 在这里可以找到更多的browsercap文件:
http://owenbrady.net/browsercaps/ http://owenbrady.net/browsercaps/
You need to place them in the App_Browsers
folder of your ASP.NET website then you can discover info about the client browser using Request.Browser
, in particular: Request.Browser.IsMobileDevice
您需要将它们放置在ASP.NET网站的
App_Browsers
文件夹中,然后可以使用Request.Browser
来发现有关客户端浏览器的信息,尤其是: Request.Browser.IsMobileDevice
I agree with @Sohnee that following this path is a maintenance hazard, but it can be useful for detecting spiders. 我同意@Sohnee的观点,遵循此路径是维护风险,但对于检测蜘蛛可能很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.