Method1: Use ASP.NET To Detect The User-Agent
Adding server-side browser detection and redirection to your website using the ASP.NET
platform is quite easy. This code should be inserted into the Page_Load
event of the web form code behind file (e.g. default.aspx.cs). To enable this site-wide,
just add it to the Page_Load event of the Master Page file.
Listing 1: Method1
In the code above you can add as many user agents as you wish. The else
statement is not necessary in this case, because we want the page to load normally,
when the request is coming from standard browsers.
Limitations of above code are:
- It will not catch all mobile browsers as there are a lot of them.
- You need to keep updating user agents when new devices are introduced.
- Not easy to parse user agents to get detailed information about the mobile device
such as the manufacturer, model, screen height & width, and image formats supported.
This type of information is necessary to customize page layout to the specific mobile
device.
These limitations made me ask "is there any better way to achieve this?" I came
across the following method.
Method2.1: Use 51Degrees.mobi .NET Mobile API To Detect The User-Agent [ASP.NET]
51Degrees.mobi
provides a free open source ASP.NET mobile API allowing Visual Basic and C# developers
to benefit from the extensive mobile device information available in WURFLalso used by the
BBC, Bank of America,
MySpace and Admob among others. WURFL device database is widely-accepted
as the most advanced and up-to-date mobile device database available.
The following steps demonstrate how to detect a mobile device, obtain accurate device
details and easily redirect to a mobile landing page overcoming the limitations
of Method 1.
Step1: Create Web Site
Note
Visual Studio 2008 default installation does not have "Mobile Web Form" template.
To develop mobile web applications the necessary templates need to be installed.
To install these templates download them from this Visual Web Developer Team Blog Post, extract the ZIP file,
and follow the instructions in the included readme.txt files attached to each of
the extracted Zip folders. Once installed please perform the following steps. Visual
Studio 2005 users do not require installing these templates as they are already
installed.
- Create a C# ASP.NET website.
- The website will be created with a default web form "Default.aspx", keep the name
as it is.
- Add a Mobile Web Form to the website using "Add New Item -> Mobile Web Form".
Name the mobile web form to "M.aspx"
Step2: 51Degrees.mobi resource download
Following files need to be added to the web site created in Step1.
- App_Data/wurfl.xml.gz
- App_Data/web_browsers_patch.xml.gz
- bin/Mobile.dll
These files can be extracted from the Enhance download available here.
Once downloaded your website should have following folder structure.
Figure 1: The Website Folder Structure

Step3: Web.config Settings
Following sections need to be added to the web.config file of your web site to make
use of the API
Configuration section
The following settings are needed at the top of the web.config file. They tell .NET
about subsequent configurations in the web.config and how to handle them. In this
instance we're telling .NET to use the Mobile assembly.
Listing 2: Web.config Setting1
Note
The version number 0.1.5.0 should be changed to match the version of the Mobile.dll
assembly you're using. All other sections should remain unchanged
Add new mobile section
Add the following mobile element after the configSections element.
These lines control how the Mobile API responds to mobile devices and where to locate
the database of mobile devices.
Listing 3: Web.config Setting2
Detector Module
Add the following element to the httpModules element. These allow the
Mobile API to intercept new page requests and redirect them if the requesting device
is a mobile.
Listing 4: Web.config Setting3
Note
The version number 0.1.5.0 should be changed to match the version of the Mobile.dll
assembly you're using. All other sections should remain unchanged
Step4: Mobile Page (M.aspx)
Add the following code to M.aspx and M.aspx.cs
Listing 5: M.aspx