使用桌面设备访问页面后,移动设备不会获得 View.Mobile.cshtml 次浏览

Mobile devices wont get View.Mobile.cshtml views after visiting page with desktop

我用 Index.cshtmlIndex.Mobile.cshtml 创建了一个 asp.net mvc 应用程序。视图使用相同的控制器和操作。在开发中,我在 phone 上获得移动视图,在桌面上获得桌面视图。一切都按预期工作。

将应用程序部署到生产服务器后,在 smartphone 上一切都按预期工作,直到我从桌面浏览器导航到该页面。从桌面浏览器导航到该页面后,我将无法再在移动设备上获得移动视图。

我禁用并启用了缓存设置,但无论哪种方式我都有这种奇怪的行为。我错过了什么吗?

已编辑:

我得到了以下主题结构:

/Themes/DefaultTheme/Views/Cart/Index.Mobile.cshtml /Themes/OceanasTheme/Views/Cart/Index.cshtml

在我的自定义 razor 视图引擎中,我修改了 ViewLocationFormats,简化了:

var customViewLocationFormats = new List<string>();
customViewLocationFormats.Add("~/Themes/DefaultTheme/Views/{1}/{0}.cshtml");
customViewLocationFormats.Add("~/Themes/OceanasTheme/Views/{1}/{0}.cshtml");

//override default view location
ViewLocationFormats = customViewLocationFormats.ToArray();

当我将 Index.Mobile.cshtml 复制到 OceanasTheme 文件夹时,出现了同样的问题,只是我每次访问桌面后都会从 OceanasTheme 获取 Mobile.сshtml。 (桌面从 OceanasTheme 获取 Index.cshtml)。

已更新至 MVC 5.2.7 并删除了 Microsoft.AspNet.Mvc.FixedDisplayModes 包。但是还是不行。

RazorViewEngine 正在使用 DefaultViewLocationCache 缓存视图。 .Mobile 视图位置在第一次请求移动设备后存储在缓存中。当使用桌面浏览器请求时,在缓存中找不到桌面变体,因此 RazorViewEngine 将尝试在磁盘上查找视图。它会在不同的文件夹中找到视图,并将其存储在缓存中,但它还会检查移动设备是否存在于该文件夹中。因为 .Mobile 不在此文件夹中,所以它将用空路径覆盖缓存。

我通过创建 IViewLocationCache 的自定义实现解决了这个问题。