ASP.NET 移动设备浏览量:未将 Firefox Mobile 检测为移动设备
ASP.NET Mobile Views: Not detecting Firefox Mobile as Mobile
我正在使用 ASP.NET MVC 5。我为某些特定场景渲染了一堆 .mobile
视图。
我注意到 Android 上的 Mozilla Firefox 移动应用程序没有获得渲染视图的 .mobile
版本,所以 ASP.NET 没有检测到该设备是移动设备。
用户代理字符串是:
Mozilla/5.0 (Android; Mobile; rv:39.0) Gecko/39.0 Firefox/39.0
有没有办法可以全局覆盖移动检测以强制执行此操作?
可以,使用 System.Web.WebPages
命名空间中的 DisplayModeProvider
。
您可以将自己的逻辑添加到 DisplayModeProvider
以确定应呈现哪个视图。
如果将此添加到启动或应用程序初始化中:
DisplayModeProvider.Instance.Modes
.Add(new DefaultDisplayMode("Mobile")
{
ContextCondition = context => {
var userAgent = context.GetOverriddenUserAgent();
return userAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) > -1
&& userAgent.IndexOf("Mobile", StringComparison.OrdinalIgnoreCase) > -1
&& userAgent.IndexOf("Firefox", StringComparison.OrdinalIgnoreCase) > -1;
}
});
这会将在用户代理字符串中包含 "Android"
、"Mobile"
和 "Firefox"
的 http 请求定向到您的后缀为 Mobile 的视图.
同样,您可以为特定设备创建特定的覆盖和视图,即 iPhone 4 的视图,iPhone 5 的另一个视图等。可以找到更多信息 here.
我正在使用 ASP.NET MVC 5。我为某些特定场景渲染了一堆 .mobile
视图。
我注意到 Android 上的 Mozilla Firefox 移动应用程序没有获得渲染视图的 .mobile
版本,所以 ASP.NET 没有检测到该设备是移动设备。
用户代理字符串是:
Mozilla/5.0 (Android; Mobile; rv:39.0) Gecko/39.0 Firefox/39.0
有没有办法可以全局覆盖移动检测以强制执行此操作?
可以,使用 System.Web.WebPages
命名空间中的 DisplayModeProvider
。
您可以将自己的逻辑添加到 DisplayModeProvider
以确定应呈现哪个视图。
如果将此添加到启动或应用程序初始化中:
DisplayModeProvider.Instance.Modes
.Add(new DefaultDisplayMode("Mobile")
{
ContextCondition = context => {
var userAgent = context.GetOverriddenUserAgent();
return userAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) > -1
&& userAgent.IndexOf("Mobile", StringComparison.OrdinalIgnoreCase) > -1
&& userAgent.IndexOf("Firefox", StringComparison.OrdinalIgnoreCase) > -1;
}
});
这会将在用户代理字符串中包含 "Android"
、"Mobile"
和 "Firefox"
的 http 请求定向到您的后缀为 Mobile 的视图.
同样,您可以为特定设备创建特定的覆盖和视图,即 iPhone 4 的视图,iPhone 5 的另一个视图等。可以找到更多信息 here.