码迷,mamicode.com
首页 > 移动开发 > 详细

MVC视图展现模式之移动布局

时间:2016-12-03 12:40:27      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:ice   ext.get   for   包含   mvc   cat   indexof   phone   页面   

参考:http://www.cnblogs.com/dunitian/p/5218140.html

 

简单点,直接上用法

新建MVC项目,在golbal.asax中添加如下代码

 

1 //添加一个自定义后缀
2 DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("iphone")
3 {
4     ContextCondition = (Context) => Context.Request.UserAgent.Contains("iphone")
5 });

 

新建一个view,增加iphone.cshtml为后缀的文件

那么如果user-agent中包含iphone,就会打开该页面!

很简单吧!

 

另外在stackoverflow上找到另外一个人的写法,基本上一样,直接上代码了

 1 DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("iPhone")
 2 {
 3     ContextCondition = context =>
 4         context.GetOverriddenBrowser().IsMobileDevice
 5         && (context.Request.UserAgent.IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) >= 0
 6         || context.Request.UserAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) >= 0
 7         || !context.Request.Browser.IsMobileDevice)
 8     });
 9 
10 /*  Looks complicated, but renders Home.iPhone.cshtml if the overriding browser is
11     mobile or if the "real" browser is on an iPhone or Android. This falls through
12     to the next instance Home.Mobile.cshtml for more basic phones like BlackBerry.
13 */
14 
15 DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("Mobile")
16 {
17     ContextCondition = context =>
18         context.GetOverriddenBrowser().IsMobileDevice
19 });

 

MVC视图展现模式之移动布局

标签:ice   ext.get   for   包含   mvc   cat   indexof   phone   页面   

原文地址:http://www.cnblogs.com/sharkyzh/p/6128229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!