參考: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 });