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

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

问题描述

在ASP.NET MVC 4一站式服务大约一个小时的运行时间后,对正确的意见,尽管浏览器

移动显示模式将覆盖正确检测覆盖的移动设备。

回收的应用程序池暂时解决问题。

新的浏览器覆盖功能正常允许移动设备查看网站的桌面版本,反之亦然。但经过约一个小时的运行时间中,移动视图不再呈现用于移动设备;只有默认的桌面剃刀模板呈现。唯一的解决办法是回收应用程序池。

奇怪的是,浏览器的cookie覆盖继续工作。一个主 _Layout.cshtml 模板正确显示移动或桌面的文字根据的值ViewContext.HttpContext.GetOverriddenBrowser()。IsMobileDevice ,但错误的观点仍然被渲染。这使我相信,问题在于DisplayModes。

在讨论的动作不会被缓存:

  [的OutputCache(NoStore = TRUE,持续时间= 0,的VaryByParam =*)]

我使用51Degrees移动侦测,但我不认为这应该不会影响覆盖的移动侦测。这是ASP.NET MVC 4 Beta版和放大器在 DisplayModes 功能中的错误;开发商preVIEW,还是我做别的事情了?


下面是我的 DisplayModes 设置在的Application_Start

  DisplayModeProvider.Instance.Modes.Insert(0,新DefaultDisplayMode(iPhone)
{
    ContextCondition =背景=>
        context.GetOverriddenBrowser()。IsMobileDevice
        &功放;&安培; (context.Request.UserAgent.IndexOf(简称iPhone,StringComparison.OrdinalIgnoreCase)GT = 0
        || context.Request.UserAgent.IndexOf(机器人,StringComparison.OrdinalIgnoreCase)GT = 0
        || !context.Request.Browser.IsMobileDevice)
    });/ *看起来很复杂,但呈现Home.iPhone.cshtml如果压倒一切的浏览器
    移动或者真正的浏览器在iPhone或Android。这落空
    到下一个实例Home.Mobile.cshtml的更基本的手机一样的黑莓手机。
* /DisplayModeProvider.Instance.Modes.Insert(1,新DefaultDisplayMode(手机)
{
    ContextCondition =背景=>
        context.GetOverriddenBrowser()。IsMobileDevice
});


解决方案

这是在MVC 4(codePLEX一个已知问题:的#280:多DisplayModes - 缓存错误,会显示错误的视图)。这将固定在MVC的下一个版本。

在此期间,你可以在这里安装可用的一种解决方法包:的http://的NuGet。组织/包/ Microsoft.AspNet.Mvc.FixedDisplayModes 的。

对于大多数应用程序只要安装这个包应该解决这个问题。

对于定制注册视图引擎收集一些应用程序,你应该确保您引用 Microsoft.Web.Mvc.FixedRazorViewEngine Microsoft.Web.Mvc.FixedWebFormViewEngine 而不是默认的视图引擎的实现。

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.

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.

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 = "*")]

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?


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
});

解决方案

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.

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.

For some applications that customize the collection of registered view engines, you should make sure that you reference Microsoft.Web.Mvc.FixedRazorViewEngine or Microsoft.Web.Mvc.FixedWebFormViewEngine, instead of the default view engine implementations.

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

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