views:

491

answers:

5

I am working on an ASP.Net MVC application that with views that I want to be able to format for desktop browsers as well as mobile browsers. Both formats would use the same Controllers and Actions but require different views.

What's the best way to do this? Do I do a browser detect in the Action and then invoke the appropriate view? Does the view do the browser detect and then render itself accordingly? Is there something in between return View(); and when the view actually is processed that I can override?

Thanks

A: 

I would detect the browser in the controller and return the appropriate view. I guess you could override the view engine but that really seems like overkill to the extreme.

Jason
That's what I was thinking but it doesn't feel right to end every Action method with if browser == mobile return view("mobile"); else return view("desktop");
Matthew
+1  A: 

I'd go with custom attributes stuffing appropriate views/masterpages around the returned Model. We use this tactic heavily for Ajaxification, shouldn't be difficult to extend the concept to mobile.

Wyatt Barnett
Interesting. Can you give me more details? Where do the attributes go? How do you use the attributes to decide what view to use? Do you have a custom view engine?
Matthew
+6  A: 

Here some links: Mobile Web Application Toolkit, mdbf. And don´t forget use WURFL.

fravelgue
Looks like the mobile web app toolkit is just what I need. I've known about MDBF and had already planned on using it. Thanks for the links!
Matthew
A: 

Hello

You can look at http://51degrees.codeplex.com it is free open source asp.net mobile api which helps is tracking whether request is coming from mobile devices and redirects user to mobile optimized .aspx pages without changing any existing web pages. It makes use of WURFL the most upto date mobile device database. Along with this it also gives you detailed capability information of mobile device making request which you can use to customize pages for each mobile device.

You can see articles for same here http://dotnetslackers.com/articles/aspnet/Mobile-Device-Detection-and-Redirection-Using-ASP-NET-MVC.aspx

Amit Patel
+4  A: 

ScottHa has this post about targeting multiple devices with ASP.NET MVC addressing that same problem.

I would suggest either in the view or in something between the controller and the view (the ViewEngine...)

Arjan Einbu