I want to dynamically change the layout, based on whether the user is accessing the site from a mobile device or not. I have a few questions on the subject.
-
I would like to change the layout once it’s determined, but before the view is rendered. That is, is there some hook I can use where I can change
layout.cshtmltolayout.mobile.cshtml"recursively" for any nested layouts that there might be.Is overriding
RazorViewEngine‘sCreateViewmethod enough? It takes amasterPathparameter, so maybe I could implement a customRazorViewEngineand override this method, right? -
The second question would be regarding mobile browser detection. I don’t really care that much about specific devices. I just want to differentiate desktop from mobile. Is something like
WURFLa necessity in this case?Is checking something like
Request.Browser.ScreenPixelsWidthandScreenPixelsHeigthridiculous? (since most I’d be changing would be using or notjQuery.mobileand it’s data-attributes.
This functionality is built-into ASP.NET MVC 4 so you get it out-of-the-box.
Scott Hansleman blogged about how you could achieve the same in ASP.NET MVC 3 and be ready for the upgrade because the NuGet he suggested is spec-compatible with ASP.NET MVC 4.