如何检测手机浏览器在.NET应用程序MVC3应用程序、浏览器、手机、NET

2023-09-02 01:52:58 作者:把悲伤留给自己。

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

I'm developing a .NET MVC3 application.

是否有,如果用户正在使用移动浏览器中(使用剃刀)的视图,以检测一个好方法。 我想不同的显示逻辑,如果它是一个移动浏览器。

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暴露了IsMobileDevice国旗在的Request.Browser对象。

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

因此​​,在您的剃须刀code,可以查询这个变量,并相应地呈现。

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