MVC4 Force Mobile网站针对包括台式机在内的所有请求 [英] MVC4 Force Mobile Site for all requests including Desktop

查看:71
本文介绍了MVC4 Force Mobile网站针对包括台式机在内的所有请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为移动设备和台式机设备创建了MVC 4 Internet应用程序项目. 现在,我只想为所有浏览器全局显示Mobile网站.

I have created an MVC 4 Internet Application project for both Mobile and Desktop devices. Now I want to just display the Mobile site globally for all browsers.

我一直在尝试使用代码HttpContext.SetOverriddenBrowser(BrowserOverride.Mobile)来执行此操作,但是无论放在哪里,它似乎都无法正常工作.

I've been trying to use the code HttpContext.SetOverriddenBrowser(BrowserOverride.Mobile) to do this, but it doesn't seem to work correctly no matter where I put it.

当前,我可以使用ViewSwitcher从桌面站点切换到移动站点,但这是不切实际的,因为桌面站点尚无法正常运行.

Currently I can switch from the desktop site to the mobile site using ViewSwitcher but this is impractical as the desktop site isn't yet functional.

public RedirectResult SwitchView(bool mobile, string returnUrl) {
    if (Request.Browser.IsMobileDevice == mobile)
        HttpContext.ClearOverriddenBrowser();
    else
        HttpContext.SetOverriddenBrowser(mobile ? BrowserOverride.Mobile : BrowserOverride.Desktop);

    return Redirect(returnUrl);
}

我所有的移动视图均采用VIEWNAME.Mobile.cshtml格式.任何帮助将不胜感激,谢谢.

All of my mobile views are in VIEWNAME.Mobile.cshtml format. Any help would be greatly appreciated, thank-you.

推荐答案

我参加聚会可能有点晚了,但是我遇到了像OP一样的问题,不喜欢Tieson的解决方案.所以我想出了一个替代方案,对我来说非常有用.

I'm maybe a bit late to the party, but I had the same problem like the OP and didn't like Tieson's solution. So I came up with an alternative, that worked great for me.

将以下代码添加到您的项目 Global.asax.cs :

Add the following code to your projects Global.asax.cs:

protected void Application_BeginRequest()
{
    Request.RequestContext.HttpContext.SetOverriddenBrowser(BrowserOverride.Mobile);
}

这会在每个请求中告诉ASP.NET,它是移动浏览器的请求.

This tells ASP.NET on every request, that it's a request of a mobile browser.

这篇关于MVC4 Force Mobile网站针对包括台式机在内的所有请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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