ASP.NET – Get IE Browser Version Using C#

In this post, we show you two approaches on how to get the version of IE browser in ASP.NET C#.

 

1. Using HttpRequest.Browser approach

IEBrowser.cs
using System;
using System.Web;

namespace ByteNota
{
    class IEBrowser
    {
        public static int? GetIEVersion(HttpContext context)
        {
            int? ieVersion = null;

            HttpBrowserCapabilities bc = context.Request.Browser;
            string browserName = bc.Browser;

            if ((browserName == "IE") || (browserName == "InternetExplorer"))
            {
                ieVersion = bc.MajorVersion;
            }

            return ieVersion;
        }
    }
}

In this approach, we use HttpRequest.Browser property, which is available in System.Web namespace. We return version if the browser is IE using HttpBrowserCapabilities.MajorVersion property.

Using HttpBrowserCapabilities.Version property if you want to get the full version of the IE browser.

 

2. Using HttpRequest.UserAgent approach

IEBrowser.cs
using System;
using System.Web;
using System.Linq;

namespace ByteNota
{
    class IEBrowser
    {
        public static int? GetIEVersion(HttpContext context)
        {
            string userAgent = context.Request.UserAgent;

            // detect IE below 11
            if (userAgent.Contains("MSIE"))
            {
                string[] parts = userAgent.Split(new string[] { "MSIE" }, StringSplitOptions.None);
                string versionString = new string(parts[1].Trim().TakeWhile(char.IsDigit).ToArray());
                int version = int.Parse(versionString);
                return version;
            }

            // detect IE 11
            if (userAgent.Contains("Trident/"))
            {
                string[] parts = userAgent.Split(new string[] { "rv:" }, StringSplitOptions.None);
                string versionString = new string(parts[1].Trim().TakeWhile(char.IsDigit).ToArray());
                int version = int.Parse(versionString);
                return version;
            }

            // not found
            return null;
        }
    }
}

In this approach, We simply look at HttpRequest.UserAgent data sent by the browser to determine whether the visitors are using IE browser and then extract version from it.

 

Use the method:

int? ieVersion = IEBrowser.GetIEVersion(context);

0 0 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x