MVC5 DisplayModeProvider注册问题

时间:2014-02-26 08:33:03

标签: mobile asp.net-mvc-5 wurfl

所以我有一个mvc 5应用程序,有3种显示模式,桌面(默认),移动设备和平板电脑。我正在使用WURFL来找出设备。这是从global.cs调用的代码注册:

public static void RegisterDisplayModes(IList<IDisplayMode> displayModes){
        var datafile = HttpContext.Current.Server.MapPath(WurflDataFilePath);
        var configurer = new InMemoryConfigurer().MainFile(datafile);
        var manager = WURFLManagerBuilder.Build(configurer);
        HttpContext.Current.Cache[WURFLMANAGER_CACHE_KEY] = manager;

        bool mobileEnabled = ConfigurationManager.AppSettings["EnableMobileSite"] == "true";
        bool tabletEnabled = ConfigurationManager.AppSettings["EnableTabletSite"] == "true";

        var modeDesktop = new DefaultDisplayMode("") {
                                                        ContextCondition = (c => c.Request.IsDesktop())
                                                    };
        var modeMobile = new DefaultDisplayMode("mobile"){
                                                             ContextCondition = (c => c.Request.IsMobile())
                                                         };
        var modeTablet = new DefaultDisplayMode("tablet"){
                                                             ContextCondition = (c => c.Request.IsTablet())
                                                         };

        displayModes.Clear();
        if (mobileEnabled) displayModes.Add(modeMobile);
        if (tabletEnabled) displayModes.Add(modeTablet);
        displayModes.Add(modeDesktop);
    }

我正在使用HttpRequestBase的一些扩展方法,如http://msdn.microsoft.com/en-us/magazine/dn296507.aspx中所述:

public static bool IsDesktop(this HttpRequestBase request){
        return true;
    }
    public static bool IsMobile(this HttpRequestBase request) {
        return IsMobileInternal(request.UserAgent) && !IsForcedDesktop(request);
    }
    public static bool IsTablet(this HttpRequestBase request) {
        return IsTabletInternal(request.UserAgent) && !IsForcedDesktop(request);
    }

    public static void OverrideBrowser(this HttpRequestBase request, bool forceDesktop){
        request.RequestContext.HttpContext.Cache[OVERRIDE_BROWSER_CACHE_KEY] = forceDesktop;
    }

    public static bool IsForcedDesktop(this HttpRequestBase request){
        var isForced = request.RequestContext.HttpContext.Cache[OVERRIDE_BROWSER_CACHE_KEY];
        return isForced != null ? isForced.ToString().ToBool() : false;
    }

    private static bool IsMobileInternal(string userAgent) {
        var device = WURFLManagerBuilder.Instance.GetDeviceForRequest(userAgent);
        if (device.IsTablet() == true) {
            return false;
        } else {
            return device.IsMobile();
        }
    }

    private static bool IsTabletInternal(string userAgent) {
        var device = WURFLManagerBuilder.Instance.GetDeviceForRequest(userAgent);
        return device.IsTablet();
    }

这一切都运行良好一段时间,但一小时左右后,移动设备和平板电脑设备开始显示桌面视图,桌面视图开始显示ViewSwitcher共享视​​图(我假设大多数人都熟悉它,它只允许您从移动设备显示桌面视图)。这几乎就像mvc4中的缓存错误。我已经尝试删除我的代码来注册显示模式,只是使用默认的mvc移动支持,并且它工作正常它有同样的问题!很明显这里有一个问题......任何人都能看到明显的东西吗?几乎不可能调试导致问题只是在很长一段时间后才开始出现,即使那时只有真正的系统!有什么想法吗?

谢谢你们......现在这个问题太长了...... 干杯 安迪

编辑:甚至将其剥离回到默认实现会产生问题。我添加了一些调试代码,以确保我实际上运行mvc5,但它似乎我。我也通过禁用缓存尝试了最初推荐的mvc4问题解决方法,仍然没有乐趣。真的没有人对此有任何信息吗?

1 个答案:

答案 0 :(得分:2)

所以我终于明白了。像往常一样简单。出于某种原因,我使用RequestContext.HttpContext.Cache来保存当有人想要完整视图而不是移动视图时的状态。我从来没有使用过HttpContext.Cache,我很确定我会从某个地方的博客那里拿到它 - 虽然不能再找到它了。所有发生的事情都是它会为每个人切换视图,而不仅仅是一个人。无法相信需要花费数周的时间来解决这个问题。希望它在某些时候帮助别人。