如何在 .NET MVC3 应用程序中检测移动浏览器 [英] How do I detect a mobile browser in a .NET MVC3 application

查看:23
本文介绍了如何在 .NET MVC3 应用程序中检测移动浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 .NET MVC3 应用程序.

I'm developing a .NET MVC3 application.

是否有检测用户是否在视图中使用移动浏览器的好方法(使用 RAZOR).如果是移动浏览器,我想改变显示逻辑.

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.

谢谢!

推荐答案

MVC3 在 Request.Browser 对象中公开了 IsMobileDevice 标志.

MVC3 exposes an IsMobileDevice flag in the Request.Browser object.

所以在你的 razor 代码中,你可以查询这个变量并相应地呈现.

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 -->
}

这篇关于如何在 .NET MVC3 应用程序中检测移动浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆