How can I detect the device from which my website is accessed?

北城余情 提交于 2019-12-22 14:01:27

问题


I have .NET website that is mainly built for desktop browsers but I'm currently making a mobile version of it. What I'm trying to do is load the website according to the device on which it is accessed by the user. Is there a way that I can get information about the user's device when they request the site?

Regards, Sumit.


回答1:


As suggested by others you can use the System.Web.HttpBrowserCapabilities class accessible through Request.Browser however without updated browser definition files the information is completely worthless.

For example, if you use Request.Browser.IsMobileDevice this should give you what you want, but is based on a set of very dated regular expressions in your .NET framework folders in Windows.

You can see the kind of problems that can be encountered on a (currently unanswered and unloved) post here on Stack Overflow.

If you do not plan on keeping the browser definition files up to date (which is no small task) then quite simply do not go down this route.

If you simply want to know whether the user is viewing on a mobile device then here are a few options:

  • Detect Mobile Browsers
  • 51Degrees.mobi
  • WURFL
  • User Agent Info

Some of those are free, some aren't and if you are really only interested in whether it is a mobile device, my recommendation would be to use Detect Mobile Browsers.




回答2:


Take a look at the HttpBrowserCapabilities class.

Enables the server to gather information on the capabilities of the browser that is running on the client.

This is actually exposed on the Request property of the Page object - in an ASP.NET page you can do the following:

var browserCap = this.Request.Browser;



回答3:


You can examine the HttpRequest.Browser property - MSDN link.




回答4:


If you are looking for something more low level: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.useragent(v=vs.90).aspx




回答5:


A commercial alernative is BrowserHawk



来源:https://stackoverflow.com/questions/8446264/how-can-i-detect-the-device-from-which-my-website-is-accessed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!