Reading
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!
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
51 Degrees.
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?
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
with it.
i was using session_startso it only detected once per session and then i store it in a session variable. how is beginRequest better? would it detect for each request? although, i guess i could check the status of the variablebefore detecting similar to a singleton
class.
dgerler
Member
3 Points
36 Posts
MVC 4 not using mobile pages
Apr 09, 2012 10:12 PM|LINK
Reading 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!
BrockAllen
All-Star
27522 Points
4901 Posts
MVP
Re: MVC 4 not using mobile pages
Apr 09, 2012 10:22 PM|LINK
What does this render in your page?
HttpContext.GetOverriddenBrowser().IsMobileDevice
This is how MVC4's display modes detects if it should render a ".Mobile" view, partial or layout template.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
dgerler
Member
3 Points
36 Posts
Re: MVC 4 not using mobile pages
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
BrockAllen
All-Star
27522 Points
4901 Posts
MVP
Re: MVC 4 not using mobile pages
Apr 09, 2012 10:35 PM|LINK
In the view, you'll need to prefix that with the ViewContext:
<h2>@ViewContext.HttpContext.GetOverriddenBrowser().IsMobileDevice</h2>
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
dgerler
Member
3 Points
36 Posts
Re: MVC 4 not using mobile pages
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?
BrockAllen
All-Star
27522 Points
4901 Posts
MVP
Re: MVC 4 not using mobile pages
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 51 Degrees.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
dgerler
Member
3 Points
36 Posts
Re: MVC 4 not using mobile pages
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?
BrockAllen
All-Star
27522 Points
4901 Posts
MVP
Re: MVC 4 not using mobile pages
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 with it.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
dgerler
Member
3 Points
36 Posts
Re: MVC 4 not using mobile pages
Apr 10, 2012 12:49 AM|LINK
BrockAllen
All-Star
27522 Points
4901 Posts
MVP
Re: MVC 4 not using mobile pages
Apr 10, 2012 12:55 AM|LINK
Well, 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 :))
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/