如何在 .NET MVC3 应用程序中检测移动浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5233090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I detect a mobile browser in a .NET MVC3 application
提问by Dave
I'm developing a .NET MVC3 application.
我正在开发一个 .NET MVC3 应用程序。
Is there a good way to detect if the user is using a mobile browser in the view (using RAZOR). I'm wanting to differ the display logic if it's a mobile browser.
是否有一种很好的方法来检测用户是否在视图中使用移动浏览器(使用 RAZOR)。如果它是移动浏览器,我想改变显示逻辑。
Thanks!
谢谢!
回答by tsiorn
MVC3 exposes an IsMobileDevice flag in the Request.Browser object.
MVC3 在 Request.Browser 对象中公开了 IsMobileDevice 标志。
So in your razor code, you can query this variable and render accordingly.
所以在你的剃刀代码中,你可以查询这个变量并相应地呈现。
For example, in your view (razor):
例如,在您看来(剃刀):
@if (Request.Browser.IsMobileDevice) {
<!-- HTML here for mobile device -->
} else {
<!-- HTML for desktop device -->
}
回答by Ryan Tofteland
The built in browser detection capabilities are no longer being kept up to date. Take a look at Scott Hanselman's blog- refer to the "More to Come" section at the bottom for details.
内置的浏览器检测功能不再保持最新。查看Scott Hanselman 的博客- 请参阅底部的“更多内容”部分了解详细信息。
From that article:
从那篇文章:
Since that post, the Live.com team in Ireland that released and supported the original Mobile Device Browser File (MDBF) has stopped producing it. The best source for mobile browser device data is WURFL (that was one of the places that MDBF pulled from.)
自那篇帖子以来,爱尔兰的 Live.com 团队发布并支持原始移动设备浏览器文件 (MDBF) 已停止生产它。移动浏览器设备数据的最佳来源是 WURFL(这是 MDBF 提取的地方之一。)
I suggest taking a look at 51Degrees.mobi for more accurate detection. Also see the Steve Sanderson blogthat Hanselman references for how to implement this in MVC3.
我建议查看 51Degrees.mobi 以获得更准确的检测。另请参阅Hanselman 参考的Steve Sanderson 博客,了解如何在 MVC3 中实现这一点。
回答by Arun Prasad E S
I use this Method (Works fine for Me)
我使用这种方法(对我来说很好用)
if (eDurar.MobileDetect.DeviceType.Any(m => Request.UserAgent.Contains(m)))
{
Layout = "~/Views/Shared/_mobileLayout.cshtml";
@Html.Partial("mobileIndex");
}
else
{
Layout = "~/Views/Shared/_Layout.cshtml";
@Html.Partial("desktopIndex");
}
I suggest you to use responsive bootstrap something, avoiding mobile specific page is better
我建议你使用响应式引导程序,避免移动特定页面更好

