Microsoft KB Archive/311281

= HOW TO: Determine the Browser Version in ASP.NET =

Article ID: 311281

Article Last Modified on 4/19/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft Internet Explorer 5.01
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q311281



The following .NET Framework Class LIbrary namespaces are referenced in this article:

System.Web.

IN THIS TASK
SUMMARY
 * Requirements
 * Determine the Browser Version

REFERENCES



SUMMARY
This article describes how to determine the Web browser version in ASP.NET.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Visual Studio .NET
 * Microsoft Internet Information Services (IIS)

This article assumes that you are familiar with the following topics:
 * Visual Studio .NET
 * Microsoft .NET Framework

back to the top

Determine the Browser Version
ASP.NET introduces the HttpBrowserCapabilities class to gather information during an HTTP request about the capabilities of the browser. HttpBrowserCapabilities informs your program about the type and level of support that the browser offers and can be accessed by querying the HttpRequest.Browser property.

The HttpBrowserCapabilities class includes properties such as Browser, MajorVersion, and MinorVersion to help describe the browser. Previously, ASP used the Browser Capabilities component (Browscap.dll) to access the capabilities of the browser by comparing the User-Agent HTTP header with the entries in the Browscap.ini file. The information in the Browscap.ini file is now stored in a configuration file in ASP.NET. By default, the browserCaps element in machine.config stores all of the settings of the Browser Capabilities componenet.

The following sample uses HttpBrowserCapabilities to determine the browser information:  Start Visual Studio .NET. Create a new ASP.NET Web Application in Microsoft Visual Basic .NET. Switch to Design view. Add a Server button and Server text box. By default, these are named Button1 and TextBox1. Change the Server text box TextMode property to MultiLine, and then extend the width and height of the text box. Double-click Button1.  In the Code window for the Button1_Click event, add the following code:

VB .NET
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim bc As HttpBrowserCapabilities Dim s As String = &quot;&quot; bc = Request.Browser With bc           s &= &quot;Browser Capabilities&quot; & vbCrLf s &= &quot;Type = &quot; & .Type & vbCrLf s &= &quot;Name = &quot; & .Browser & vbCrLf s &= &quot;Version = &quot; & .Version & vbCrLf s &= &quot;Major Version = &quot; & .MajorVersion & vbCrLf s &= &quot;Minor Version = &quot; & .MinorVersion & vbCrLf s &= &quot;Platform = &quot; & .Platform & vbCrLf s &= &quot;Is Beta = &quot; & .Beta & vbCrLf s &= &quot;Is Crawler = &quot; & .Crawler & vbCrLf s &= &quot;Is AOL = &quot; & .AOL & vbCrLf s &= &quot;Is Win16 = &quot; & .Win16 & vbCrLf s &= &quot;Is Win32 = &quot; & .Win32 & vbCrLf s &= &quot;Supports Frames = &quot; & .Frames & vbCrLf s &= &quot;Supports Tables = &quot; & .Tables & vbCrLf s &= &quot;Supports Cookies = &quot; & .Cookies & vbCrLf s &= &quot;Supports VB Script = &quot; & .VBScript & vbCrLf s &= &quot;Supports JavaScript = &quot; & .JavaScript & vbCrLf s &= &quot;Supports Java Applets = &quot; & .JavaApplets & vbCrLf s &= &quot;Supports ActiveX Controls = &quot; & .ActiveXControls & vbCrLf End With TextBox1.Text = s End Sub

Visual C#
private void Button1_Click(object sender, System.EventArgs e) { HttpBrowserCapabilities bc; string s;       bc = Request.Browser; s= &quot;Browser Capabilities&quot; + &quot;\n&quot;; s += &quot;Type = &quot; + bc.Type + &quot;\n&quot;; s += &quot;Name = &quot; + bc.Browser + &quot;\n&quot;; s += &quot;Version = &quot; + bc.Version + &quot;\n&quot;; s += &quot;Major Version = &quot; + bc.MajorVersion + &quot;\n&quot;; s += &quot;Minor Version = &quot; + bc.MinorVersion + &quot;\n&quot;; s += &quot;Platform = &quot; + bc.Platform + &quot;\n&quot;; s += &quot;Is Beta = &quot; + bc.Beta + &quot;\n&quot;; s += &quot;Is Crawler = &quot; + bc.Crawler + &quot;\n&quot;; s += &quot;Is AOL = &quot; + bc.AOL + &quot;\n&quot;; s += &quot;Is Win16 = &quot; + bc.Win16 + &quot;\n&quot;; s += &quot;Is Win32 = &quot; + bc.Win32 + &quot;\n&quot;; s += &quot;Supports Frames = &quot; + bc.Frames + &quot;\n&quot;; s += &quot;Supports Tables = &quot; + bc.Tables + &quot;\n&quot;; s += &quot;Supports Cookies = &quot; + bc.Cookies + &quot;\n&quot;; s += &quot;Supports VB Script = &quot; + bc.VBScript + &quot;\n&quot;; s += &quot;Supports JavaScript = &quot; + bc.JavaScript + &quot;\n&quot;; s += &quot;Supports Java Applets = &quot; + bc.JavaApplets + &quot;\n&quot;; s += &quot;Supports ActiveX Controls = &quot; + bc.ActiveXControls + &quot;\n&quot;; TextBox1.Text = s;

}                   </li> Run the project and click the button. The text box lists information about your browser.</li></ol>

back to the top

<div class="references_section">