我正在使用ASP.NET MVC5,并且在某些特定情况下渲染了一堆.Mobile视图。
我注意到,Android上的Mozilla Firefox移动应用程序未呈现该视图的.Mobile版本,因此,就像ASP.NET未检测到该设备是移动设备一样。
UA字符串是Mozilla / 5.0(Android; Mobile; rv:39.0)Gecko / 39.0 Firefox / 39.0
有什么方法可以全局覆盖移动检测以强制执行此操作?
最佳答案
是的,可以使用DisplayModeProvider
命名空间中的System.Web.WebPages
。
您可以将自己的逻辑添加到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。
关于c# - ASP.NET Mobile View -未将Firefox Mobile检测为Mobile,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31628794/