Export (0) Print
Expand All

How to: Detect Browser Types and Browser Capabilities 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 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"
            + "Supports JavaScript Version = " +
                browser["JavaScriptVersion"] + "\n";
    
        TextBox1.Text = s;
    }
    

Community Additions

ADD
Show:
© 2014 Microsoft