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

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

简单点,直接上用法

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

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

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

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

很简单吧!

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

 DisplayModeProvider.Instance.Modes.Insert(, new DefaultDisplayMode("iPhone")
{
ContextCondition = context =>
context.GetOverriddenBrowser().IsMobileDevice
&& (context.Request.UserAgent.IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) >=
|| context.Request.UserAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) >=
|| !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(, new DefaultDisplayMode("Mobile")
{
ContextCondition = context =>
context.GetOverriddenBrowser().IsMobileDevice
});
上一篇:ASP.NET MVC---自定义HtmlHelper方法


下一篇:Spring MVC视图解析器