ASP.NET MVC 4 移动显示模式停止工作 [英] ASP.NET MVC 4 Mobile Display Modes Stop Working

查看:20
本文介绍了ASP.NET MVC 4 移动显示模式停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC 4 中的移动显示模式在正常运行约一小时后停止提供正确的视图,尽管浏览器覆盖正确检测到被覆盖的移动设备.

Mobile display modes in ASP.NET MVC 4 stop serving the correct views after about an hour of uptime, despite browser overrides correctly detecting an overridden mobile device.

回收应用程序池暂时解决了问题.

Recycling the application pool temporarily solves the problem.

新的浏览器覆盖功能正确地允许移动设备查看网站的桌面版本,反之亦然.但是在大约一个小时的正常运行时间之后,移动视图不再为移动设备呈现;仅呈现默认的桌面 Razor 模板.唯一的解决办法是回收应用程序池.

The new browser override feature correctly allows mobile devices to view the desktop version of a site, and vice-versa. But after about an hour of uptime, the mobile views are no longer rendered for a mobile device; only the default desktop Razor templates are rendered. The only fix is to recycle the application pool.

奇怪的是,浏览器覆盖 cookie 继续起作用.主 _Layout.cshtml 模板根据 ViewContext.HttpContext.GetOverriddenBrowser().IsMobileDevice 的值正确显示移动"或桌面"文本,但错误的视图是仍在渲染中.这让我相信问题出在 DisplayModes 上.

Strangely, the browser override cookie continues to function. A master _Layout.cshtml template correctly shows "mobile" or "desktop" text depending on the value of ViewContext.HttpContext.GetOverriddenBrowser().IsMobileDevice, but the wrong views are still being rendered. This leads me to believe the problem lies with the DisplayModes.

有问题的操作没有被缓存:

The action in question is not being cached:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

我使用 51Degrees 进行移动检测,但我认为这不会影响覆盖的移动检测.这是 ASP.NET MVC 4 Beta & 的 DisplayModes 功能中的错误吗?开发者预览版,还是我做错了什么?

I am using 51Degrees for mobile detection, but I don't think this should affect the overridden mobile detection. Is this a bug in DisplayModes feature for ASP.NET MVC 4 Beta & Developer Preview, or am I doing something else wrong?

这是我在 Application_Start 中的 DisplayModes 设置:

Here is my DisplayModes setup in 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
});

推荐答案

这是 MVC 4 (Codeplex: #280:多个显示模式 - 缓存错误,将显示错误的视图).这将在下一个 MVC 版本中修复.

This is a known issue in MVC 4 (Codeplex: #280: Multiple DisplayModes - Caching error, will show wrong View). This will be fixed in the next version of MVC.

与此同时,您可以在此处安装一个解决方法包:http://nuget.org/packages/Microsoft.AspNet.Mvc.FixedDisplayModes.

In the meantime you can install a workaround package available here: http://nuget.org/packages/Microsoft.AspNet.Mvc.FixedDisplayModes.

对于大多数应用程序,只需安装此软件包即可解决问题.

For most applications simply installing this package should resolve the issue.

对于一些自定义已注册视图引擎集合的应用程序,您应该确保引用 Microsoft.Web.Mvc.FixedRazorViewEngine Microsoft.Web.Mvc.FixedWebFormViewEngine代替默认的视图引擎实现.

这篇关于ASP.NET MVC 4 移动显示模式停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆