Sdílet prostřednictvím


How to: Detect Browser Types in ASP.NET Web Pages

Different browsers and different versions of the same browsers support different features. In your application, you might need to determine what type of browser a user is viewing pages with, and perhaps determine if the browser supports certain features.

NoteNote

ASP.NET can automatically determine browser capabilities and use this information to render appropriate HTML markup for ASP.NET server controls. For details, see ASP.NET Server Controls and Browser Capabilities.

To detect browser types in an ASP.NET page

  • Query the Browser property, which contains an HttpBrowserCapabilities object. This object gets information from the browser or client device during an HTTP request, telling your application the type and level of support the browser or client device offers. The object in turn exposes information about browser capabilities using strongly typed properties and a generic name-value dictionary.

    The following code example shows how you can displays browser information the information in a text box on the page.

    NoteNote

    The properties exposed by the HttpBrowserCapabilities object indicate inherent capabilities of the browser, but do not necessarily reflect current browser settings. For example, the Cookies property indicates whether a browser inherently supports cookies, but it does not indicate whether the browser that made the request has cookies enabled. For more information, see ASP.NET Cookies Overview.

    Private Sub Button1_Click(ByVal sender As System.Object, _
           ByVal e As System.EventArgs) Handles Button1.Click
        Dim s As String = ""
        With Request.Browser
            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 VBScript = " & .VBScript & vbCrLf
            s &= "Supports JavaScript = " & _
                .EcmaScriptVersion.ToString() & vbCrLf
            s &= "Supports Java Applets = " & .JavaApplets & vbCrLf
            s &= "Supports ActiveX Controls = " & .ActiveXControls & _
                vbCrLf
        End With
        TextBox1.Text = s
    End Sub
    
    private void Button1_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";
        TextBox1.Text = s;
    }
    

See Also

Concepts

ASP.NET Web Server Controls and Browser Capabilities