Framework / Web Forms / Response Types / Browser Detection

In This Topic
    Browser Detection
    In This Topic
     Overview

    As we mentioned in the previous topic browser detection is based on two objects called NBrowser and NBrowserResponsePair.

    The NBrowser object describes a browser by using a combination of a regular expression match (wildcard character string) and version number. This is how it works:

    The NBrowser object contains two string properties called MatchExpression and NoMatchExpression . In order for a browser match to happen the first expression must evaluate to true and the second one must evaluate to false. If the expression string is empty no match is performed and the control assumes that the expression evaluated to true in the case of the MatchExpression or to false in the case of the NoMatchExpression one respectively. To illustrate this in practice lets examine a few expression that match to the most common browsers on the Internet:

     1. Microsoft Internet Explorer (IE)

    The HTTP User agent string send by IE looks like:

    Mozilla/4.0 (compatible; MSIE 4.01; Windows95)

    Therefore the MatchExpression and NoMatchExpression string must contain:

     

    C#
    Copy Code
    MatchExpression = "compatible*IE*";
    NoMatchExpression = "";
    
     2. Netscape

    Netscape browsers send a HTTP User agent string similar to:

    Mozilla/4.04 [en] (Win95; I)

    Obviously this string does not contain the "compatible" word. Therefore the wildcard expressions we'll have to use are:

    C#
    Copy Code
    MatchExpression = "*Mozilla*";
    NoMatchExpression = "*compatible*";
    
     3. Opera

    The Opera browser HTTP User agent string looks like: 

    Mozilla/3.0 (compatible; Opera/3.0; Windows95/NT4)

    In this case the NoMatchExpression regular expression must be again empty as in the case of IE:

    C#
    Copy Code
    MatchExpression = "*compatible*Opera*";
    NoMatchExpression = "";
    
     
    

    As you can see from the above examples the combination of MatchExpression and NoMatchExpression can virtually distinguish any kind of browser. It can also help you to specify groups of browsers for example:

     

    1. All browsers other than IE:

    C#
    Copy Code
    MatchExpression = "";
    NoMatchExpression = "compatible*IE*";
    

    2. All browsers other than Netscape:

    C#
    Copy Code
    MatchExpression = "*compatible*";
    NoMatchExpression = "";
    

    In addition to the MatchExpression and NoMatchExpression properties the NBrowser object also provides means to distinguish between different browser versions. This is accomplished with the help of the FromMajorVersion , FromMinorVersion , ToMajorVersion , ToMinorVersion and MatchIfBiggerVersion properties.

     

    The first four define an interval of versions [FromMajorVersion.FromMinorVersion, ToMajorVersion.ToMinorVersion] in which the browser version should fall in. When MatchIfBiggerVersion is set to true then the upper boundary is discarded and the interval is [FromMajorVersion.FromMinorVersion, infinity).

    To finish the discussion on Browser detection lets look at a few lines of real code:

     

    1. Instructs the control to generate a JPEG static image if the page is accessed from Netscape browsers version 5.0 and above.

    C#
    Copy Code
    NBrowser browser = new NBrowser();
    browser.MatchExpression = "*Mozilla*";
    browser.NoMatchExpression = "*compatible*";
    
    // all other browsers must have compatible.
    browser.FromMajorVersion = 5;
    browser.FromMinorVersion = 0;
    browser.MatchIfBiggerVersion = true;
    
    NImageResponse imageResponse = new NImageResponse();
    imageResponse.ImageFormat = new NJpegImageFormat();
    
    NBrowserResponsePair browserResponsePair = new NBrowserResponsePair();
    browserResponsePair.Response = imageResponse;
    browserResponsePair.Browser = browser;
    //[theComponent] is a valid instance of either NChartControl or NDrawingView
    [theComponent].ServerSettings.BrowserResponseSettings.BrowserResponsePairs.Add(browserResponsePair);
    
    Visual Basic
    Copy Code
    Dim browser As New NBrowser
    browser.MatchExpression = "*Mozilla*"
    browser.NoMatchExpression = "*compatible*"
    
    ' all other browsers must have compatible.
    browser.FromMajorVersion = 5
    browser.FromMinorVersion = 0
    browser.MatchIfBiggerVersion = True
    
    Dim imageResponse As New NImageResponse
    imageResponse.ImageFormat = New NJpegImageFormat
    
    Dim browserResponsePair As New NBrowserResponsePair
    browserResponsePair.Response = imageResponse
    browserResponsePair.Browser = browser
    '[theComponent] is a valid
    instance of either
    NChartControl or NDrawingView
    [theComponent].ServerSettings.BrowserResponseSettings.BrowserResponsePairs.Add(browserResponsePair)
    

    2. Instructs the control to generate a PNG static image if the page is accessed from IE browsers version 5.0 and above.

    C#
    Copy Code
    NBrowser browser = new NBrowser();
    browser.MatchExpression = "*MSIE*";
    browser.NoMatchExpression = "";
    
    browser.FromMajorVersion = 5;
    browser.FromMinorVersion = 0;
    browser.MatchIfBiggerVersion = true;
    
    NImageResponse imageResponse = new NImageResponse();
    
    NBrowserResponsePair browserResponsePair = new NBrowserResponsePair();
    browserResponsePair.Response = imageResponse;
    browserResponsePair.Browser = browser;
    //[theComponent] is a valid instance of either NChartControl or NDrawingView
    [theComponent].ServerSettings.BrowserResponseSettings.BrowserResponsePairs.Add(browserResponsePair);
    
    Visual Basic
    Copy Code
    Dim browser As NBrowser =  New NBrowser() 
    browser.MatchExpression = "*MSIE*"
    browser.NoMatchExpression = ""
    
    browser.FromMajorVersion = 5
    browser.FromMinorVersion = 0
    browser.MatchIfBiggerVersion = True
    
    Dim imageResponse As NImageResponse =  New NImageResponse() 
    
    Dim browserResponsePair As NBrowserResponsePair =  New NBrowserResponsePair() 
    browserResponsePair.Response = imageResponse
    browserResponsePair.Browser = browser
    '[theComponent] is a valid instance of either NChartControl or NDrawingView
    [theComponent].ServerSettings.BrowserResponseSettings.BrowserResponsePairs.Add(browserResponsePair)
    
    See Also