Click here to Skip to main content
Licence CPOL
First Posted 14 Sep 2009
Views 11,820
Bookmarked 22 times

How to Detect Browser Capabilities in ASP.NET

By | 14 Sep 2009 | Article
This project demonstrates the implementation of server-side Web Browser's type/capability detection API, written in C# and some Java scripting

Introduction

Proper detection of Web Browsers' type and capabilities is rather important from the end-user's and developer's prospective as well. The differences between browsers, rooted in either branding or versioning create browser compatibility issues, which could cause some Web applications to run incorrectly if the compatibility requirements were not met.

A simple browser-detection feature could be added to ASP.NET web applications in order to resolve potential compatibility issues and also to help the tech support folks to perform remote troubleshooting based on the clients' feedback regarding the browser they use.

A working DEMO is available at www.webinfocentral.com (refer to the button control named “Check your Browser”). A sample screenshot is shown below in Fig.1:

Figure 1. Sample screenshot demonstrating browser capabilities

Background

This project demonstrates a simple implementation of server-side browser type/capabilities detection feature, written in C# with just a little bit of client-side Java scripting added for better responsiveness and interactivity. The same or even better level of responsiveness/interactivity could be achieved by using either ASP.NET AJAX or Microsoft Silverlight™ RIA technology set instead of JavaScript.

Using the Code

The project (ASP.NET 2.0+ web site) contains:

  • Class Module "BrowserInfo.cs" to be placed in AppCode directory
  • Default Web page "Default.aspx" to be placed in Application root directory

The core functionality is encapsulated in the class module "BrowserInfo.cs" shown below (notice some optional extensions, commented-off in the code; also, for simplicity and readability this solution is using simple string concatenation. Some performance improvement could be achieved by implementing StringBuilder object for string manipulation):

//******************************************************************************
// Module           :   BrowserInfo.cs
// Author           :   Alexander Bell
// Copyright        :   2008-2009 Infosoft International Inc
// Date Created     :   01/15/2008
// Last Modified    :   09/14/2009
// Description      :   Get Browser info
//******************************************************************************
// DISCLAIMER: This Application is provide on AS IS basis without any warranty
//******************************************************************************
using System;
using System.Web;
///*****************************************************************************
/// <summary>Server sider Browser detection: ASP.NET 2.0</summary> 
public static class InfosoftBrowserInfo
{
    #region Get Browser capabilities
    /// <summary>Browser capabilities: 2D array of Name/Values</summary>
    public static string[,] BrowserAttributes
    {
        get
        {
            string _agent = String.Empty;
            if (HttpContext.Current == null) return null;
            try
            {
                // detailed string describing some of browser capabilities
                _agent = HttpContext.Current.Request.UserAgent;
                // browser capabilities object
                HttpBrowserCapabilities _browser = HttpContext.Current.Request.Browser;
                // browser capabilities (properties) 2D array of strings, Name/Value
                string[,] arrFieldValue = 
                {
                    {
                    //"Type",
                    "Name",
                    "Version",
                    //"Major Version",
                    //"Minor Version",
                    "Platform",
                    "ECMA Script Version",
                    "Is Mobile Device",
                    "Is Beta",
                    //"Is Crawler",
                    //"Is AOL",
                    "Is Win16",
                    "Is Win32",
                    "Supports Frames",
                    "Supports Tables",
                    "Supports Cookies",
                    "Supports CSS",
                    "Supports VB Script",
                    "Supports JavaScript",
                    "Supports Java Applets",
                    "Supports ActiveX Controls",
                    "Supports CallBack",
                    "Supports XMLHttp",
                    
                    String.Empty,
                    "User Agent Details"
                    }, 
                    {
                    //_browser.Type,
                    
                    (_agent.ToLower().Contains("chrome"))? "Chrome" :_browser.Browser,
                    (_agent.ToLower().Contains("chrome"))? 
			"See User Agent Details below" :_browser.Version,
                    
                    //_browser.MajorVersion.ToString(),
                    //_browser.MinorVersion.ToString(),
                    
                    _browser.Platform,
                    _browser.EcmaScriptVersion.ToString(),
                    
                    (_browser.IsMobileDevice)? "YES": "NO",
                    (_browser.Beta)? "YES": "NO",
                    
                    //_browser.Crawler.ToString(),
                    //_browser.AOL.ToString(),
                    (_browser.Win16)? "YES": "NO",
                    (_browser.Win32)? "YES": "NO",
                    
                    (_browser.Frames)? "YES": "NO",
                    (_browser.Tables)? "YES": "NO",
                    (_browser.Cookies)? "YES": "NO",
                    (_browser.SupportsCss)? "YES": "NO",
                    (_browser.VBScript)? "YES": "NO",
                    (_browser.JavaScript)? "YES": "NO",
                    (_browser.JavaApplets)? "YES": "NO",
                    (_browser.ActiveXControls)? "YES": "NO",
                    (_browser.SupportsCallback)? "YES": "NO",
                    (_browser.SupportsXmlHttp)? "YES": "NO",
                    String.Empty,
                    _agent
                    }
                };
                return arrFieldValue;
            }
            catch { return null; }
        }
    }
    /// <summary>JavaScript string to containing Browsers capabilities</summary>
    public static string BrowserJavaScript
    {
        get
        {
            // return string contains JavaScript
            string MsgBrowser = String.Empty;
            string[,] arrBrowser = BrowserAttributes;
            if (arrBrowser == null) return String.Empty;
            try
            {
                // pop-up message using JavaScript:alert function
                MsgBrowser = "javascript:alert('Your Browser properties: \\n";
                for (int i = 0; i < arrBrowser.GetLength(1); i++)
                { MsgBrowser += "\\n" + arrBrowser[0, i] + " : " + arrBrowser[1, i]; }
                return MsgBrowser += "')";
            }
            catch { return String.Empty; }
        }
    }
    #endregion
}

A Sample Web Page contains a single Button1 control with "onclick" event handler added as shown below:

 <%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Browser Detection } Infosoft International Inc</title>
    
    <script runat="server">
        private string _browser;
        protected void Page_PreInit(object sender, EventArgs e)
        {_browser = InfosoftBrowserInfo.BrowserJavaScript;}
        protected void Page_Load(object sender, EventArgs e)
        { Button1.Attributes.Add("onclick", _browser);}
    </script>
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Get Browser Info" />
        <br />
    </div>
    </form>
</body>
</html>

Points of Interest

The solution is tested for compatibility with four major Web browsers:

  • Microsoft Internet Explorer 7.0+
  • Mozilla Firefox
  • Google Chrome
  • Apple Safari

History

  • Article posted on 09/14/2009

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

DrABELL

Chief Technology Officer
Infosoft Int'l
United States United States

Member

Dr. A. Bell (aka DrABELL), hi-tech consultant living in the US, has more than 20 years of software and electronic engineering experience. He published more than 100 technical articles and authored 37 inventions. Dr. Bell is the Windows/Internet technologies veteran, currently focused on: .NET,Mobile,Java,C#,SQL,HTML5,CSS3. He developed the most popular Silverlight Media Player (#1 Goog) and 3 Fractions Calculator (also #1 on Google/Bing/Yahoo). Sample online projects:
  1. Embedded youTube Player: ASP.NET API
  2. Semantic Analyzer
Popular hi-tech articles:
  1. WebTV Project: Embedded YouTube Player
  2. Personal computer 2011: mostly USB 3.0, SATA 3.0, DDR 3 plus SSD/HDMI
  3. Introducing the unit of internet social network efficiency: 1 Zuck
  4. Internet leader 2010: Facebook. See the entire Top10 list
  5. How to select web browser and check its capabilities
  6. SQL generates large data sequence
  7. Aggregate Product function extends SQL
  8. HTML 5, CSS 3 and Inflation Calculator
  9. RIA: embedding YouTube
  10. RIA: Silverlight™ media player
  11. RIA: HTML 5 video player


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralShort URL PinmemberDrABELL6:13 3 Jan '11  
GeneralBrowser capabilities for Mobile PinmemberAmaresh Jois4:29 27 Aug '10  
GeneralOr you can just use my page, and know even more about the browser.... Pinmemberzachgotnosauce8:38 11 Jan '10  
General[Message Deleted] PinmemberShadowX3:15 18 Sep '09  
General[My vote of 1] vote 1 Pinmembermambo_24:04 15 Sep '09  
GeneralRe: [My vote of 1] vote 1 RE: you seem to be biased and unfair [modified] PinmemberDrABELL4:52 15 Sep '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 15 Sep 2009
Article Copyright 2009 by DrABELL
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid