一个自定义的移动显示模式和桌面模式在ASP.NET MVC4之间切换 [英] Switching between a custom mobile display mode and desktop mode in ASP.NET MVC4
问题描述
我想创建一个开关整个网站的链接,并切换到移动链接。我不想强迫它使用一个会话变量去其一。我想知道,如果有可能通过使用ViewSwitcher控制器自动完成。
在这里,我使用我的定制手机的显示模式是什么
公共类MobileDisplayMode:DefaultDisplayMode
{
公共静态只读表<串GT; MobileList =新的List<串GT;
{
Android的,
移动,
歌剧牧高笛
三星,
HTC,
诺基亚,
爱立信
索尼爱立信,
苹果手机
,iPod的
塞班
,机器人
Windows CE的
,黑莓
,棕榈
Opera Mini的
}; 公共MobileDisplayMode()
:基地(手机)
{
ContextCondition =(上下文= GT; IsMobile(上下文,context.GetOverriddenUserAgent()));
} 私人布尔IsMobile(HttpContextBase背景下,串useragentString)
{
返回context.Request.Browser.IsMobileDevice || MobileList.Any(VAL => useragentString.IndexOf(VAL,StringComparison.InvariantCultureIgnoreCase)> = 0);
}
}
下面是我的看法切换code - 不用担心IsMobile属性,它的处理方式为移动显示模式相同
如果(IsMobile ==移动)
{
HttpContext.ClearOverriddenBrowser();
}
其他
{
HttpContext.SetOverriddenBrowser(?移动BrowserOverride.Mobile:BrowserOverride.Desktop);
}
添加Jquery.Mobile.Mvc包到项目中。移动布局将被创建。现在每移动视图显示一个链接到桌面版本。把下面桌面视图code,所以用户可以返回到移动
@ Html.ActionLink(移动视图,通过SwitchView,ViewSwitcher,新的移动{=真,RETURNURL = Request.Url.PathAndQuery},{新的rel =外部})
I would like to create a switch to full site link and switch to mobile link. I don't want to force it to go to either of them by using a session variable. I'd like to know if it's possible to do it automatically by using ViewSwitcher Controller.
Here is what I am using for my custom mobile display mode
public class MobileDisplayMode : DefaultDisplayMode
{
public static readonly List<string> MobileList = new List<string>
{
"Android",
"Mobile",
"Opera Mobi",
"Samsung",
"HTC",
"Nokia",
"Ericsson",
"SonyEricsson",
"iPhone"
,"ipod"
, "symbian"
,"android"
,"windows ce"
,"blackberry"
,"palm"
,"opera mini"
};
public MobileDisplayMode()
: base("Mobile")
{
ContextCondition = (context => IsMobile(context, context.GetOverriddenUserAgent()));
}
private bool IsMobile(HttpContextBase context, string useragentString)
{
return context.Request.Browser.IsMobileDevice || MobileList.Any(val => useragentString.IndexOf(val, StringComparison.InvariantCultureIgnoreCase) >= 0);
}
}
Here is my view switcher code - don't worry about IsMobile Property, it's handled the same way as mobile display mode
if (IsMobile == mobile)
{
HttpContext.ClearOverriddenBrowser();
}
else
{
HttpContext.SetOverriddenBrowser(mobile ? BrowserOverride.Mobile : BrowserOverride.Desktop);
}
Add Jquery.Mobile.Mvc package to the project. A mobile layout will be created. Now every mobile view displays a link to the desktop version. Put the code below in desktop view, so the user can returns to mobile.
@Html.ActionLink("Mobile view", "SwitchView", "ViewSwitcher", new { mobile = true, returnUrl = Request.Url.PathAndQuery }, new { rel = "external" })
这篇关于一个自定义的移动显示模式和桌面模式在ASP.NET MVC4之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!