如何在.NET MVC3应用程序中检测移动浏览器

时间:2021-09-26 07:16:40

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!

谢谢!

3 个解决方案

#1


69  

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

#2


21  

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的博客 - 详情请参阅底部的“更多信息”部分。

I suggest taking a look at 51Degrees.mobi for more accurate detection. Also see the Steve Sanderson blog that Hanselman references for how to implement this in MVC3.

我建议看看51Degrees.mobi以获得更准确的检测。另请参阅Steve Sanderson博客,Hanselman在MVC3中如何实现这一点。

#3


0  

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

我建议你使用响应式引导程序,避免移动特定页面更好

#1


69  

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

#2


21  

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的博客 - 详情请参阅底部的“更多信息”部分。

I suggest taking a look at 51Degrees.mobi for more accurate detection. Also see the Steve Sanderson blog that Hanselman references for how to implement this in MVC3.

我建议看看51Degrees.mobi以获得更准确的检测。另请参阅Steve Sanderson博客,Hanselman在MVC3中如何实现这一点。

#3


0  

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

我建议你使用响应式引导程序,避免移动特定页面更好