gpt4 book ai didi

c# - ASP.NET 移动 View : Not detecting Firefox Mobile as Mobile

转载 作者:行者123 更新时间:2023-11-30 16:06:54 25 4
gpt4 key购买 nike

我正在使用 ASP.NET MVC 5。我为某些特定场景呈现了一堆 .mobile View 。

我注意到 Android 上的 Mozilla Firefox 移动应用程序没有获取渲染 View 的 .mobile 版本,所以 ASP.NET 没有检测到该设备是移动设备.

用户代理字符串是:

Mozilla/5.0 (Android; Mobile; rv:39.0) Gecko/39.0 Firefox/39.0

有没有办法可以全局覆盖移动检测来强制执行此操作?

最佳答案

是的,您可以使用 System.Web.WebPages 命名空间中的 DisplayModeProvider

您可以将自己的逻辑添加到 DisplayModeProvider 以确定应呈现哪个 View 。

如果将其添加到启动或应用程序初始化中:

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 请求定向到您的 View 后缀为 Mobile

同样,您可以为特定设备创建特定的覆盖和 View ,即 iPhone 4 的 View ,iPhone 5 的另一个 View 等。可以找到更多信息 here .

关于c# - ASP.NET 移动 View : Not detecting Firefox Mobile as Mobile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31628794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com