MVC視圖展現模式之移動布局

來源:互聯網
上載者:User

標籤: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 is11     mobile or if the "real" browser is on an iPhone or Android. This falls through12     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().IsMobileDevice19 });

 

MVC視圖展現模式之移動布局

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.