Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting IE11 with C#

Tags:

browser

c#

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).

like image 870
Riain McAtamney Avatar asked Aug 22 '13 08:08

Riain McAtamney


2 Answers

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.

like image 123
Scott Munro Avatar answered Oct 28 '22 00:10

Scott Munro


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.

like image 33
Pawel Wrobel Avatar answered Oct 28 '22 02:10

Pawel Wrobel