13

Before loading a webpage I am detecting browser and version to determine compatibility.

So if the browser is less than IE7 I display an incompatible message.

Testing the webpage in IE11 my webpage is displaying the incompatible message.

I'm currently getting the browser name from:

var browser = Request.Browser.Browser;

and the version from

var version = Request.Browser.Version;

I then check that the browser is IE and the version >= 7.

I believe that the user agent has changed for IE11. So what is the best way to detect if the browser is >= IE7 using C#.

EDIT:

Request.Browser.Browser returns the browser name, e.g. IE. Request.Browser.Version returns the version number. I add these to a BrowserVersion object I have and compare these values to an array of supported browser versions that I have also. i.e.

private static List<BrowserVersion> m_supportedBrowsers = new List<BrowserVersion>()
        {
            new BrowserVersion("IE", 7),
            new BrowserVersion("Firefox", 3),
            new BrowserVersion("AppleMAC-Safari", 5),
            new BrowserVersion("Safari", 5)
        };

where BrowserVersion is just an object that has 2 string properties (name and version).

3
  • 7
    check for functionality and not browser itself.
    – Tigran
    Commented Aug 22, 2013 at 8:53
  • "I then check that the browser is IE and the version >= 7." - then what happens? How do you check this? "I believe that the user agent has changed for IE11" - have you tried finding the user agent string, or the way Request.Browser works?
    – CodeCaster
    Commented Aug 22, 2013 at 9:23
  • The problem with functionality testing is that they have also hidden the ActiveXObject property, which is what some might be testing for.
    – Jarrod
    Commented Feb 17, 2014 at 18:04

5 Answers 5

15

I just installed IE11 for Windows 7 Release Preview and my code also broke. After some quick testing I noticed that the string for the browser is now different. It is returned as "InternetExplorer" now instead of "IE".

This code seems to work for me now.

private void CheckIfUsingSupportedBrowser(HttpContext context)
{
    bool isBrowserSupported = ((context.Request.Browser.Browser == "IE" || context.Request.Browser.Browser == "InternetExplorer") && context.Request.Browser.MajorVersion >= 7) ||
                                (context.Request.Browser.Browser == "Firefox" && context.Request.Browser.MajorVersion >= 13) ||
                                (context.Request.Browser.Browser == "AppleMAC-Safari" && context.Request.Browser.MajorVersion >= 5) ||
                                (context.Request.Browser.Browser == "Safari" && context.Request.Browser.MajorVersion >= 5) ||
                                (context.Request.Browser.Browser == "Chrome" && context.Request.Browser.MajorVersion >= 13);

    if (!isBrowserSupported) 
    {
        Navigator.Navigate(PageKeys.SupportedBrowsers);
    }
}
3
11

Yes the user agent has changed to this:

Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko

Most important part here is the removal of MSIE token and addition of like Gecko. This means that Internet Explorer would prefer to be identified as a Gecko-type browser if it’s not identified as itself (so the old IE hacks will not be applied to it). If you want to identify it as IE than you must look for Trident token and the version comes via rv token.

Now all of the above should be taken into consideration only if you need to examine the user agent at the server side for some reason. For using JavaScript fallbacks, HTML5 polyfills etc. you should be checking if given functionality is supported in the code on the client side (there are libraries which help with that like for example Modernizr).

6

I ve been experimenting with IE11 and MVC and it turns out that IE11 identifies itself as Request.Browser.Browser = "Mozilla" and MajorVersion = 0. Hope it helps.

3

The Request.Browser information is based on the browser definition files which are located here on my machine.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\Browsers

The one for Internet Explorer is named ie.browser.

I can see there that Internet Explorer 11 and later are handled quite differently to all of the previous versions. For the previous versions, there is a base definition named IE which is in turn based on Mozilla.

<browser id="IE" parentID="Mozilla">

There is a dependency chain for all of the previous versions that can be traced back to IE. Here is one part of that chain as an example.

<browser id="IE10Plus" parentID="IE6Plus">

Internet Explorer 11 on the other hand, has a different ancestry and is based directly on Mozilla.

<browser id="InternetExplorer" parentID="Mozilla">

IE and therefore all of the versions prior to Internet Explorer 11 (none of which override this value) use the following definition for the browser capability.

<capability name="browser"              value="IE" />

Internet Explorer 11 and later user the following.

<capability name="browser"              value="InternetExplorer" />

To summarize, if you are interested in any version of Internet Explorer then you would need to use something similar to the following.

Request.Browser.Browser == "IE" || Request.Browser.Browser == "InternetExplorer"

To then identify a specific version, you would reference the Request.Browser.Version property. This is populated straight from the user agent string that the browser passes. Not again here though, that there is a difference between Internet Explorer 11 and later and previous versions.

//Versions prior to Internet Explorer 11.
<userAgent match="MSIE (?'version'(?'major'\d+)(\.(?'minor'\d+)?)(?'letters'\w*))(?'extra'[^)]*)" />

//Internet Explorer 11 and later.
<userAgent match="Trident/(?'layoutVersion'[7-9]|0*[1-9]\d+)(\.\d+)?;(.*;)?\s*rv:(?'version'(?'major'\d+)(\.(?'minor'\d+)))" />

The version is the bit after MSIE for previous versions and the bit after rv: for Internet Explorer and later.

A recent version of the .Net Framework should include the correct browser definition files but it looks like some of the older ones may require a hotfix to get it.

Update: I referred to Internet Explorer 11 and later versions through the text above. Later versions are probably Edge. I have not so far seen a browser definition file for that but suspect that it will be different again.

0

Which version of the .NET Framework are you using? From my analysis, it looks like versions less than 4.5 cannot recognize IE 11 as an Internet Explorer browser.

Not the answer you're looking for? Browse other questions tagged or ask your own question.