6

I've read that its bad (not advised) to use User Agent Sniffing to send down the correct content for a mobile browser, so I'm wondering what IS the best way to do this?

I'm using ASP.NET MVC, and I've built my site and it works well on desktop browsers, so I'm looking to begin building a mobile version. When a mobile browser comes to my site, I'd like to use a different set of Views, which ideally posses the following attributes:

  1. Link to pre-scaled images
  2. Use minimal javascript
  3. Remove all but essential content

My first thought was to sniff the user agent, and then send down a different .CSS file, but as stated above I've read that this is a bad way to do this, so I'm asking you for your thoughts.

Nate
  • 30,286
  • 23
  • 113
  • 184
  • 1
    possible duplicate of [Detecting Requests from Mobile Browsers in ASP.NET](http://stackoverflow.com/questions/2653765/detecting-requests-from-mobile-browsers-in-asp-net) – John Farrell Aug 16 '10 at 23:40
  • @jfar, MVC has specific requirements for this. See the Hanselman post. AFAICS, the answer you link doesn't cover that. – Craig Stuntz Aug 17 '10 at 22:17
  • You can see some solutions here: http://stackoverflow.com/questions/6844020/way-to-do-content-adaptation-to-mobile –  Aug 17 '11 at 13:06

4 Answers4

7

The user agent is really all you have in a HTTP GET request, but you should let someone else maintain the list. We use the Microsoft Mobile Device Browser File with a custom view engine in a manner roughly similar to this Scott Hanselman post.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • Thanks, this is what I kinda thought, guess I misunderstood the reason I was told not to. I completely agree that maintaining a compatibility list is a PITA! – Nate Aug 16 '10 at 21:12
2

The best way to detect a mobile browser is to use this wonderful codeplex project:

http://mdbf.codeplex.com/

For background on how you could create targeted views read here:

http://www.hanselman.com/blog/MixMobileWebSitesWithASPNETMVCAndTheMobileBrowserDefinitionFile.aspx

Keith Adler
  • 20,880
  • 28
  • 119
  • 189
1

The simplest approach could be use a separate domain "m.yourdomain.com" or "yourdomain.mobi" (Source) that way you can assume that the user is on a mobile device.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • In this case, would I use a browser agent and a cookie to redirect once, and if they came back, assume I've mislabeled their user agent and let them continue on full site? – Nate Aug 16 '10 at 21:05
  • This approach can be painful. You should assume anyone going to m.website.com wants the mobile site but always give them a link to the full site in the footer. – Keith Adler Aug 16 '10 at 21:15
  • @Nissan - that's a good idea, though the more I think about it the more I'm convinced my answer is not necessarily a good idea. – ChrisF Aug 16 '10 at 21:24
  • @Nate - you could do or just have a link to the mobile site on the main site, though I'm not convinced by my answer any more ;) – ChrisF Aug 16 '10 at 21:26
  • I meant have the redirect on my www. domain, redirect once to m. domain. I agree, having separate domains is a fairly safe option. I think I'm going to use the mobile browser file, since it seems pretty useful. – Nate Aug 16 '10 at 22:02
  • 1
    Don't ever redirect a deep link to a mobile home page, though. That breaks external links and irritates mobile users. – Craig Stuntz Aug 17 '10 at 12:50
0

While I believe it's frowned upon to sniff for browser to determine capability and you should use capability sniffing, such as JQuery.support. When it comes to actually presenting significantly different layouts then I think you have to sniff for the browser ID and act accordingly.

Lazarus
  • 41,906
  • 4
  • 43
  • 54
  • That works *after the page is loaded.* When responding to a `GET` request on the server, however, there's no way to do this. – Craig Stuntz Aug 16 '10 at 21:18
  • @Craig - Of course, I had my head thinking from client-side but the issue is processing from server-side where you obviously only have the browser ID supplied during the GET. – Lazarus Aug 17 '10 at 08:58