Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 10, 2012 07:45 PM by BrockAllen
Apr 09, 2012 10:12 PM|LINK
http://www.asp.net/mvc/tutorials/mvc-4/aspnet-mvc-4-mobile-features in section "Overriding Views, Layouts, and Partial Views" it says you can use an alternate file name in the form _Layout.Mobile.cshtml and it should use the new layout when accessed by
a mobile device. I have copied the original _layout file to a _Layout.Mobile and added text to the Mobile version so I can tell that it is being used. I also added the text "Hello PC" to the original layout so I could tell for certain my changes were in the
file. However, it's not using the new layout page.
I published it to a production server (MVC4 install) and it still shows me the orginal version with "Hello PC". I also print out the useragent and it seems to be accurate.
I don't know what I am doing wrong. I have attempted this in a few different ways. This is my third attempt to use MVC and produce mobile views. The first was with MVC 3, second with MVC 4 started as a Mobile application and now MVC 4 Internet Application.
Any help is appreciated!
Apr 09, 2012 10:22 PM|LINK
What does this render in your page?
This is how MVC4's
display modes detects if it should render a ".Mobile" view, partial or layout template.
Apr 09, 2012 10:30 PM|LINK
I get... 'System.Web.HttpContext' does not contain a definition for 'GetOverriddenBrowser'
when I put @HttpContext.GetOverriddenBrowser().IsMobileDevice in my index.cshtml or _Layout.cshtml
Apr 09, 2012 10:35 PM|LINK
In the view, you'll need to prefix that with the ViewContext:
Apr 09, 2012 10:51 PM|LINK
Coming up false in the emulator. I'm using the Opera Emulator.
Published it to my production server and it's giving me a false in my android broswer also. Where can I find the code that changes the view?
Apr 09, 2012 10:56 PM|LINK
I've seen the same behavior from the Opera emulator -- I didn't dig into it, but I'm guessing the ASP.NET mobile detection code just doesn't know about those other devices (but I don't know for certain).
In Chrome in the dev tools you can set the UserAgent string for iPhone. Also, for FireFox you can install the
User Agent Switcher.
For an updated list of browsers, check out
Apr 09, 2012 11:02 PM|LINK
I have done some work using www.handsetdetection.com and it seems to work pretty well. Where should I integrate it? Could I set the isMobile property in Global.asax after detecting it in the session_start method?
Apr 09, 2012 11:22 PM|LINK
Yea, you'd want to do something along those lines. I'd prefer to see it as a HttpModule in the BeginRequest event (that's as early as possible). I'd search around and see if either this company or someone else has already built a http module that integrates
Apr 10, 2012 12:49 AM|LINK
Apr 10, 2012 12:55 AM|LINK
session is evil... but other than that it's just a more general way to approach it. You can use session if you want (but read the post :))