Detect Mobile Device using ASP.NET

Not many developer are aware that there is a property available in .NET called the HttpCapabilitiesBase.IsMobileDevice that lets you detect Mobile Device Browsers. Just use the following code:

C#

using System;
using System.Web;
using System.Web.Configuration;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpBrowserCapabilities hbc = Request.Browser;
if (((HttpCapabilitiesBase)hbc).IsMobileDevice)
{
Response.Write("You are browsing from a mobile device");
}
else
{
Response.Write("No mobile device");
}
}
}

VB.NET

Imports System
Imports System.Web
Imports System.Web.Configuration

Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim hbc As HttpBrowserCapabilities = Request.Browser
If (CType(hbc, HttpCapabilitiesBase)).IsMobileDevice Then
Response.Write("You are browsing from a mobile device")
Else
Response.Write("No mobile device")
End If
End Sub
End Class

This entry was posted in ASP.Net, Syndicated. Bookmark the permalink.

Comments are closed.