2

I am currently working with mvc 4 and interested in implementing the DisplayModes feature which sniffs the browsers accessing the site and renders an appropriate view based upon the browser/device.

For example, code to check for iphone and render the applicable view:

DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("iPhone")
{
    ContextCondition = (context => context.Request.UserAgent.IndexOf
        ("iPhone", StringComparison.OrdinalIgnoreCase) >= 0)
});

I have read that out of the box it sniffs for mobile devices, and adds the mobile name to the view.

I want to create a similar, bit for tablets. Has anyone implemented this or instructions of how to do it? I can go check the user agent etc for tablet, but this feature is hit and miss so wondering if this has been achieved by anyone as of yet.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
amateur
  • 43,371
  • 65
  • 192
  • 320

2 Answers2

0

There's no such notion as tablet in HTTP. There are user agents. Tablets use different user agents. Here's a list of some of them. So all you have to do is adapt the code you have shown to match substrings of those user agents against particular devices.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks will check this out. Just want to confirm in saying that I don't need to do same for mobile as this is in built? – amateur Dec 27 '12 at 23:35
0

This is a really good overview and example available on ASP.Net called ASP.NET MVC 4 Mobile Features. Pretty much goes over the entire set of DisplayModes with examples.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150