Thanks to larger and larger screen resolution and increasing processing power, the latest smartphones can blissfully display regular websites. But is this really what users want? Typically, regular websites are too rich with information; in most cases, the font is too small and nearly impossible to read. Following links can be as challenging as catching a fly. Sure, users can browse a website using a smartphone, but at the cost of continuously zooming in and out pages and relying on the browser's ability to reflow text quickly and effectively in the actual screen real estate.
Not all websites have a mobile version and not all of them need one. In general, having a mobile site with certain features is part of a company's mobile strategy and relates to the business model and the vision that management has for it. How would you take visitors to your mobile site after it has been deployed to production?
One option requires that visitors explicitly point to a specific mobile website such as m.yoursite.com. Another option is that your website detects mobile devices and automatically redirects them to a mobile site, or to the section of the regular site that can serve content optimized for mobile devices. As a user, I prefer to be automatically redirected to content tailor-made for the device rather than having to type the mobile site URL myself. As an ASP.NET developer, how would you redirect visitors to another site? At first this doesn't seem like such a complex task. Rocket science isn't required, but the devil can be found in the details. Let's find out more.
Detecting Mobile Browsers
Detecting Mobile BrowsersIn ASP.NET, you have a simple way to determine whether the browser that placed the request is running on a mobile device or a desktop. You just check the value of the IsMobileDevice property exposed by the Request.Browser object and take a route based on the Boolean value you receive. The MSDN documentation says the IsMobileDevice property returns true if the browser is a recognized mobile device. And that's the sore point. The logic the property uses to detect devices is not really accurate and it fails on quite a few browsers running on the latest mobile devices. Without beating around the bush, let me say that the default implementation is not useful; a replacement is required.
The code to write is not complex. You need an HTTP module that intercepts incoming requests as early as possible and parses the user agent string identifying the capabilities of the browser and the underlying device. The problem here isn't writing the HTTP module; the problem is figuring out the information in the user agent string.
Today, an interesting number of ASP.NET sites use a specific framework for browser detection—51Degrees.mobi. You can obtain the framework from http://51degrees.codeplex.com or directly from Visual Studio as a NuGet package. The 51Degrees.mobi framework is based on an HTTP module that intercepts requests and determines a device's capabilities. In doing so, the framework also updates and, in some cases, extends the information in the browser object. If you have NuGet installed in Visual Studio, all you have to do is locate the package in the library manager. Figure 1 shows the window you see from within an ASP.NET project when you choose to add a package reference.

Figure 1 The Visual Studio package list