Search code examples
asp.net-mvc-3mobileviewenginewurfl

Dynamic layout for mobile visitors on ASP.NET MVC 3?


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.

  1. 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.cshtml to layout.mobile.cshtml "recursively" for any nested layouts that there might be.

    Is overriding RazorViewEngine's CreateView method enough? It takes a masterPath parameter, so maybe I could implement a custom RazorViewEngine and override this method, right?

  2. 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 WURFL a necessity in this case?

    Is checking something like Request.Browser.ScreenPixelsWidth and ScreenPixelsHeigth ridiculous? (since most I'd be changing would be using or not jQuery.mobile and it's data-attributes.


Solution

  • 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.