如何检测移动浏览器并将适当的内容指向它?

时间:2020-11-25 20:07:58

I've read that its bad (not advised) to use User Agent Sniffing to send down the correct content for a mobile browser, so I'm wondering what IS the best way to do this?

我已经读过使用用户代理嗅探向移动浏览器发送正确内容的不好(不建议),所以我想知道最好的方法是什么?

I'm using ASP.NET MVC, and I've built my site and it works well on desktop browsers, so I'm looking to begin building a mobile version. When a mobile browser comes to my site, I'd like to use a different set of Views, which ideally posses the following attributes:

我正在使用ASP.NET MVC,我已经构建了我的网站,它在桌面浏览器上运行良好,所以我希望开始构建移动版本。当移动浏览器进入我的网站时,我想使用一组不同的视图,理想情况下它们具有以下属性:

  1. Link to pre-scaled images
  2. 链接到预先缩放的图像

  3. Use minimal javascript
  4. 使用最小的JavaScript

  5. Remove all but essential content
  6. 删除所有必要的内容

My first thought was to sniff the user agent, and then send down a different .CSS file, but as stated above I've read that this is a bad way to do this, so I'm asking you for your thoughts.

我的第一个想法是嗅探用户代理,然后发送一个不同的.CSS文件,但如上所述,我已经读到这是一个不好的方法,所以我问你的想法。

4 个解决方案

#1


7  

The user agent is really all you have in a HTTP GET request, but you should let someone else maintain the list. We use the Microsoft Mobile Device Browser File with a custom view engine in a manner roughly similar to this Scott Hanselman post.

用户代理实际上是HTTP GET请求中的所有内容,但您应该让其他人维护该列表。我们使用Microsoft移动设备浏览器文件和自定义视图引擎,其方式大致类似于此Scott Hanselman帖子。

#2


2  

The best way to detect a mobile browser is to use this wonderful codeplex project:

检测移动浏览器的最佳方法是使用这个精彩的codeplex项目:

http://mdbf.codeplex.com/

For background on how you could create targeted views read here:

有关如何创建目标视图的背景,请阅读:

http://www.hanselman.com/blog/MixMobileWebSitesWithASPNETMVCAndTheMobileBrowserDefinitionFile.aspx

#3


1  

The simplest approach could be use a separate domain "m.yourdomain.com" or "yourdomain.mobi" (Source) that way you can assume that the user is on a mobile device.

最简单的方法可以是使用单独的域“m.yourdomain.com”或“yourdomain.mobi”(源),这样您就可以假设用户在移动设备上。

#4


0  

While I believe it's frowned upon to sniff for browser to determine capability and you should use capability sniffing, such as JQuery.support. When it comes to actually presenting significantly different layouts then I think you have to sniff for the browser ID and act accordingly.

虽然我认为嗅闻浏览器以确定功能是不礼貌的,你应该使用功能嗅探,例如JQuery.support。当涉及到实际呈现明显不同的布局时,我认为你必须嗅探浏览器ID并采取相应的行动。

#1


7  

The user agent is really all you have in a HTTP GET request, but you should let someone else maintain the list. We use the Microsoft Mobile Device Browser File with a custom view engine in a manner roughly similar to this Scott Hanselman post.

用户代理实际上是HTTP GET请求中的所有内容,但您应该让其他人维护该列表。我们使用Microsoft移动设备浏览器文件和自定义视图引擎,其方式大致类似于此Scott Hanselman帖子。

#2


2  

The best way to detect a mobile browser is to use this wonderful codeplex project:

检测移动浏览器的最佳方法是使用这个精彩的codeplex项目:

http://mdbf.codeplex.com/

For background on how you could create targeted views read here:

有关如何创建目标视图的背景,请阅读:

http://www.hanselman.com/blog/MixMobileWebSitesWithASPNETMVCAndTheMobileBrowserDefinitionFile.aspx

#3


1  

The simplest approach could be use a separate domain "m.yourdomain.com" or "yourdomain.mobi" (Source) that way you can assume that the user is on a mobile device.

最简单的方法可以是使用单独的域“m.yourdomain.com”或“yourdomain.mobi”(源),这样您就可以假设用户在移动设备上。

#4


0  

While I believe it's frowned upon to sniff for browser to determine capability and you should use capability sniffing, such as JQuery.support. When it comes to actually presenting significantly different layouts then I think you have to sniff for the browser ID and act accordingly.

虽然我认为嗅闻浏览器以确定功能是不礼貌的,你应该使用功能嗅探,例如JQuery.support。当涉及到实际呈现明显不同的布局时,我认为你必须嗅探浏览器ID并采取相应的行动。