Published: 05 Nov 2009
By: Amit Patel

This article explains approaches for ASP.NET mobile development to determine if an HTTP request is coming from a mobile phone and then redirecting the request to a page optimized for a mobile browser.

Contents [hide]

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

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

Listing 6: M.aspx.cs

Step5: Build the Website

Build the website using "Build -> Build Web Site" menu

Step6: Download Mobile Emulators To Test Web site

Please click here to get details for downloading Mobile Emulators to test website.

Result

When the website is accessed from a Mobile Emulator it will automatically display "M.aspx" to the user instead of "Default.aspx". Unlike Method1 you do not have to write any code for redirection, it is taken care by the 51degrees.mobi ASP.NET Mobile API. Apart from this ASP.NET Mobile API also gives information of device capabilities which can be used for customization.

Figure 2: Mobile Device Redirection Output

Mobile Device Redirection Output

Download

To download source code for the above sample application explained in Method2.1 please below:

VS2005
VS2008

Method2.2: Use 51Degrees.mobi .NET Mobile API To Detect The User-Agent [MVC]

I'm going to use a simple application to help illustrate how 51Degrees.mobi ASP.NET Mobile API can be used in an ASP.NET MVC application to detect mobile device capabilities.

Note

You'll need either Visual Studio 2008 or Visual Web Developer 2008 Express to build an ASP.NET MVC application. You also need to download the ASP.NET MVC framework from http://www.asp.net/mvc/download/

Step1: Create A New ASP.NET MVC Application

The ASP.NET MVC Framework includes Visual Studio Project Templates that make it easy to create a new web application with it. Simply select the File->New Project menu item and choose the "ASP.NET MVC Web Application" template to create a new web application using it.

Figure 3: The New Project dialog box

The New Project dialog box

Note

Make sure that you select .NET Framework 3.5 from the dropdown list at the top of the New Project dialog or the ASP.NET MVC Web Application project template won't appear.

Whenever you create a new MVC Web Application project, Visual Studio prompts you to create a separate unit test project (see Figure 04). Because we won't be creating tests in this article select the No option and click the OK button.

Figure 4: The Create Unit Test Project dialog

The Create Unit Test Project dialog

When you create a new ASP.NET MVC application with Visual Studio, you get a sample application by default. It has a standard set of folders: a Models, Views, and Controllers folder. You can see this standard set of folders in the Solution Explorer window (see Figure 05).

Figure 5: MVC Application Default Folder Structure

MVC Application Default Folder Structure

We'll need to add files/folders to Views and Controllers folders in order to build the Mobile device detection application.

In the Solution Explorer window, right-click the Views folder and select the menu option Add, New Folder. Name the new folder as Mobile (see Figure 06).

Figure 6: Adding A New Mobile View Folder

Adding A New Mobile View Folder

Step2: 51Degrees.mobi resource download

Note

Same as explained for Method 2.1 above.

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

Note

Same as explained for Method 2.1 above.

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.

Detector Module

Note

Same as explained for Method 2.1 above.

Step4: Creating The ASP.NET MVC Controller

The next step is to create the ASP.NET MVC controller. A controller is responsible for controlling how a user interacts with an ASP.NET MVC application.

Follow these steps:

  • In the Solution Explorer window, right-click the Controllers folder and select the menu option Add, Controller.
  • In the Add Controller dialog, enter the name MobileController and do not check the checkbox labeled Add action methods for Create, Update, and Details scenarios (see Figure 07).
  • Click the Add button to add the new controller to your project.

Figure 7: Adding a new ASP.NET MVC Controller

Adding a new ASP.NET MVC Controller

Listing 7: Controllers\MobileController.cs

Now we need ASP.NET MVC to look for different views if the device is a mobile. Add following code to HomeController.cs in order to load views optimized for mobile.

Listing 8: Listing: Controllers\HomeController.cs

Step5: Creating The ASP.NET MVC View

The Index() method in the MobileController.cs returns a view named Index under Views-> Mobile folder. We need to create this view for mobiles apart from Nokia, Iphone and Blackberry.

Follow these steps:

  • Right-click the Index() method in the code editor and select the menu option Add View (see Figure 08).
  • In the Add View dialog, verify that the none of the checkboxes are checked (see Figure 09).

Figure 8: Adding a view from controller action

Adding a view from controller action

Figure 9: Creating a new view with the Add View dialog

Creating a new view with the Add View dialog

After you complete these steps, a new view named Index.aspx is added to the Views\Mobile folder. Follow same steps for methods Nokia(), Iphone() and Blackberry() to create views (see Figure 10).

Figure 10: Views->Mobile folder structure

Views->Mobile folder structure

The contents of the Index view are included in below Listing.

Listing 9: Listing: Views\Mobile\Index.aspx

Use the same method to add the html code for the views Nokia.aspx, Iphone.aspx and BlackBerry.aspx. You can find code from the sample application attached to this article (see below for download link).

Step6: Build the Application using Build menu

Build the application using the Build menu.

Step7: Download Mobile Emulators To Test Web site

Please click here to get details for downloading Mobile Emulators to test website.

Result

When the website is accessed it will check whether it is accessed from mobile device or desktop/laptop. If it is accessed from a desktop/laptop than Views\Home\Index.aspx view will be dispalyed. If it is accessed from mobile a device a view optimized for requesting mobile device will be displayed. e.g. If requesting mobile device is Nokia than Views\Mobile\Nokia.aspx will be displayed.

51Degrees.mobi gives detailed capability information(like manufacturer, model, screen height & width, image supports and many more) of the mobile requesting website which can be very handy for designing ASP.NET MVC Views for different device family (Nokia, Iphone, Blackberry etc.).

Figure 11: Different MVC views loaded for different devices with device capability information.

Different MVC views loaded for different devices with device capability information.

Download

To download source code for the above sample MVC application explained in Method2.2 please click here

Conclusion

If you're developing mobile websites and struggling with the variety of mobile devices use Method2.1 or Method2.2 as explained above. It will reduce development time, uses device data you can trust and leaves you free to focus on delivering an amazing mobile experience. Both methods will detect network transcoders and detect devices extremely quickly. Method 2.1 will work with .NET version 2.0 and above allowing mobile pages to be developed alongside existing sites without requiring an upgrade. Method 2.2 works with MVC and .NET version 3.5 and above allowing you to use Microsoft's latest technologies

Resources

  • Click here for more details on .NETMobile API.
  • Click here for detailed information on web.config settings for .NETMobile API usage.
<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Amit Patel

Sorry, no bio is available

This author has published 3 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Code First Approach using Entity Framework 4.1, Inversion of Control, Unity Framework, Repository and Unit of Work Patterns, and MVC3 Razor View
A detailed introduction about the code first approach using Entity Framework 4.1, Inversion of Contr...
jQuery Mobile ListView
In this article, we're going to look at what JQuery Mobile uses to represent lists, and how capable ...
Exception Handling and .Net (A practical approach)
Error Handling has always been crucial for an application in a number of ways. It may affect the exe...
JQuery Mobile Widgets Overview
An overview of widgets in jQuery Mobile.
Book Review: SignalR: Real-time Application Development
A book review of SignalR by Simone.

You might also be interested in the following related blog posts


ASP.NET Mobile Content read more
Shared Source Microsoft Device Emulator 1.0 Release read more
Webcast follow up read more
Go XBAP, WPF/E and Big Screen Photos! Aussie Aussie Aussie... read more
Countdown to the Microsoft Origami: More Buzz, More Suspense read more
Top
 
 
 

Please login to rate or to leave a comment.