ASP.NET MVC 4中的移动显示模式在正常运行大约一个小时后停止提供正确的视图,尽管浏览器重写了正确检测被覆盖的移动设备。
回收应用程序池暂时解决了这个问题。
新的浏览器覆盖功能允许移动设备查看站点的桌面版本,反之亦然。但是,大约一个小时的正常运行时间后,移动视图不再呈现给移动设备;只有默认的桌面Razor模板被呈现。唯一的解决办法是回收应用程序池。
奇怪的是,浏览器覆盖cookie继续工作。主_Layout.cshtml模板根据ViewContext.HttpContext.GetOverriddenBrowser().IsMobileDevice的值正确显示“移动”或“桌面”文本,但仍呈现错误的视图。这使我相信问题在于DisplayModes。
所涉及的行动没有被缓存:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]我使用51 51Degrees进行移动检测,但我不认为这会影响覆盖的移动检测。这是DisplayModes MVC4Beta& Developer的ASP.NET特性中的一个bug,还是我做错了什么?
以下是我在DisplayModes中的Application_Start设置
DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("iPhone")
{
ContextCondition = context =>
context.GetOverriddenBrowser().IsMobileDevice
&& (context.Request.UserAgent.IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) >= 0
|| context.Request.UserAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) >= 0
|| !context.Request.Browser.IsMobileDevice)
});
/* Looks complicated, but renders Home.iPhone.cshtml if the overriding browser is
mobile or if the "real" browser is on an iPhone or Android. This falls through
to the next instance Home.Mobile.cshtml for more basic phones like BlackBerry.
*/
DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("Mobile")
{
ContextCondition = context =>
context.GetOverriddenBrowser().IsMobileDevice
});发布于 2012-09-14 23:46:48
这是MVC 4 (Codeplex:#280:多个DisplayModes缓存错误,将显示错误的视图)中的一个已知问题。这将在MVC的下一个版本中解决。
同时,您可以安装一个可以在这里使用的解决方案包:http://nuget.org/packages/Microsoft.AspNet.Mvc.FixedDisplayModes。
对于大多数应用程序来说,简单地安装这个包就可以解决这个问题。
对于一些自定义注册视图引擎集合的应用程序,您应该确保引用的是 Microsoft.Web.Mvc.FixedRazorViewEngine 或 Microsoft.Web.Mvc.FixedWebFormViewEngine,,而不是默认的视图引擎实现.。
发布于 2012-07-04 10:59:19
我也有过类似的问题,当我将基于webforms的桌面视图和基于剃须刀的移动视图混合在一起时,结果发现这是一个错误。
有关更多信息,请参见http://aspnetwebstack.codeplex.com/workitem/276
发布于 2012-07-16 01:33:30
可能是ASP.NET MVC 4中与视图缓存有关的bug,请参阅:
http://forums.asp.net/p/1824033/5066368.aspx/1?Re+MVC+4+RC+Mobile+View+Cache+bug+
https://stackoverflow.com/questions/9354188
复制相似问题