Getting Browser Information and Capabilities in ASP.Net

While developing a web site or web application, one need to keep in mind the features supported by different browsers. This is very unfortunate that there is no industry standard in the browser market. Fortunately ASP.net provides HttpBroserCapability class to determine the browser type and features supported by the browser.

The browser property of request object returns the object of HttpBrowserCapability object. This object has several properties which provide the information about the browser type, version and features supported by this.

Following C# code explains how you can use HttpBrowserCapability class to fetch browser information.

private void btnBrowserInfo_Click(object sender, System.EventArgs e)
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = “Browser Capabilities\n”
+ “Type = ”                    + browser.Type + “\n”
+ “Name = ”                    + browser.Browser + “\n”
+ “Version = ”                 + browser.Version + “\n”
+ “Major Version = ”           + browser.MajorVersion + “\n”
+ “Minor Version = ”           + browser.MinorVersion + “\n”
+ “Platform = ”                + browser.Platform + “\n”
+ “Is Beta = ”                 + browser.Beta + “\n”
+ “Is Crawler = ”              + browser.Crawler + “\n”
+ “Is AOL = ”                  + browser.AOL + “\n”
+ “Is Win16 = ”                + browser.Win16 + “\n”
+ “Is Win32 = ”                + browser.Win32 + “\n”
+ “Supports Frames = ”         + browser.Frames + “\n”
+ “Supports Tables = ”         + browser.Tables + “\n”
+ “Supports Cookies = ”        + browser.Cookies + “\n”
+ “Supports VBScript = ”       + browser.VBScript + “\n”
+ “Supports JavaScript = ”     +
browser.EcmaScriptVersion.ToString() + “\n”
+ “Supports Java Applets = ”   + browser.JavaApplets + “\n”
+ “Supports ActiveX Controls = ” + browser.ActiveXControls
+ “\n”;
txtBrowserInfo.Text = s;
}

I have placed one button on the aspx page and named it as Browser Information. The id of the button is btnBrowserInfo. On the click event of the button, I wrote the above code.

I also placed a TextBox on the page and named it as txtBrowserInfo.

First I initialized an HttpBrowserCapability object named browser with the Request object’s browser property. I fetched the different property of browser and concatenate these into a string variable. Finally I displayed the concatenated string into the text box.

kick it on DotNetKicks.com

Advertisements

One thought on “Getting Browser Information and Capabilities in ASP.Net

  1. Hi Yanesh.

    This is really easy dude in ASP.NET.. In ASP also we had same capabilities using . HTTP_USER_AGENT is a server variable. For example: One can check which browser is used by following ASP script:

    If InStr(1, Request.ServerVariables(“HTTP_USER_AGENT”), “MSIE”) then
    Response.Write “You are using Internet Explorer”
    Else
    Response.Write “You are not using Internet Explorer”
    End If

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s