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 = ""
bc = Request.Browser
With bc
s &= "Browser Capabilities" & vbCrLf
s &= "Type = " & .Type & vbCrLf
s &= "Name = " & .Browser & vbCrLf
s &= "Version = " & .Version & vbCrLf
s &= "Major Version = " & .MajorVersion & vbCrLf
s &= "Minor Version = " & .MinorVersion & vbCrLf
s &= "Platform = " & .Platform & vbCrLf
s &= "Is Beta = " & .Beta & vbCrLf
s &= "Is Crawler = " & .Crawler & vbCrLf
s &= "Is AOL = " & .AOL & vbCrLf
s &= "Is Win16 = " & .Win16 & vbCrLf
s &= "Is Win32 = " & .Win32 & vbCrLf
s &= "Supports Frames = " & .Frames & vbCrLf
s &= "Supports Tables = " & .Tables & vbCrLf
s &= "Supports Cookies = " & .Cookies & vbCrLf
s &= "Supports VB Script = " & .VBScript & vbCrLf
s &= "Supports JavaScript = " & .JavaScript & vbCrLf
s &= "Supports Java Applets = " & .JavaApplets & vbCrLf
s &= "Supports ActiveX Controls = " & .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= "Browser Capabilities" + "\n";
s += "Type = " + bc.Type + "\n";
s += "Name = " + bc.Browser + "\n";
s += "Version = " + bc.Version + "\n";
s += "Major Version = " + bc.MajorVersion + "\n";
s += "Minor Version = " + bc.MinorVersion + "\n";
s += "Platform = " + bc.Platform + "\n";
s += "Is Beta = " + bc.Beta + "\n";
s += "Is Crawler = " + bc.Crawler + "\n";
s += "Is AOL = " + bc.AOL + "\n";
s += "Is Win16 = " + bc.Win16 + "\n";
s += "Is Win32 = " + bc.Win32 + "\n";
s += "Supports Frames = " + bc.Frames + "\n";
s += "Supports Tables = " + bc.Tables + "\n";
s += "Supports Cookies = " + bc.Cookies + "\n";
s += "Supports VB Script = " + bc.VBScript + "\n";
s += "Supports JavaScript = " + bc.JavaScript + "\n";
s += "Supports Java Applets = " + bc.JavaApplets + "\n";
s += "Supports ActiveX Controls = " + bc.ActiveXControls + "\n";
TextBox1.Text = s;
}
- Run the project and click the button. The text box lists
information about your browser.
back to the top
REFERENCES
For more information, visit the following Microsoft Web
sites.
The HttpBrowserCapabilities members:
The browserCaps element:
For additional information,
click the article number below to view the article in the Microsoft Knowledge
Base:
167820 HOWTO: Determine Browser Version from a Script
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:
back to the top