Mobile layout of MVC View display mode and mvc View display Layout
Reference: http://www.cnblogs.com/dunitian/p/5218140.html
Easy to use
Create an MVC project and add the following code to golbal. asax:
1 // Add a custom suffix 2 DisplayModeProvider. instance. modes. insert (0, new defadisplaydisplaymode ("iphone") 3 {4 ContextCondition = (Context) => Context. request. userAgent. contains ("iphone") 5 });
Create a new view and add an iphone. cshtml extension file.
If the user-agent contains the iphone, the page is displayed!
Easy!
In addition, on stackoverflow, find another person's statement, basically the same, and directly go to the code.
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 });