Browser detection

I need to separate IE and FF browsers from others

it’s a pseudo-code :

If (CurrentBrowser == IE(6+) or FF(2+) )
{
...
}
else 
{
...
}

in protected void Page_Load() event (think so)

if ((Request.Browser.Type == "IE") || (Request.Browser.Type == "FF"))
{
    WebMsgBox.Show("1111");
}

no effects :-/ what is IE and FF types?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

if (Request.Browser.Type.Contains("Firefox")) // replace with your check
{
    ...
} 
else if (Request.Browser.Type.ToUpper().Contains("IE")) // replace with your check
{
    if (Request.Browser.MajorVersion  < 7)
    { 
        DoSomething(); 
    }
    ...
}
else { }

Method 2

Here’s a way you can request info about the browser being used, you can use this to do your if statement

System.Web.HttpBrowserCapabilities browser = Request.Browser;
    string s = "Browser Capabilitiesn"
        + "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";

MSDN Article

Method 3

Try the below code

HttpRequest req = System.Web.HttpContext.Current.Request
string browserName = req.Browser.Browser;

Method 4

    private void BindDataBInfo()
    {
        System.Web.HttpBrowserCapabilities browser = Request.Browser;
        Literal1.Text = "<table border="1" cellspacing="3" cellpadding="2">";
        foreach (string key in browser.Capabilities.Keys)
        {
            Literal1.Text += "<tr><td>" + key + "</td><td>" + browser[key] + "</tr>";
        }
        Literal1.Text += "</table>";
        browser = null;
    }

Method 5

I would not advise hacking browser-specific things manually with JS. Either use a javascript library like “prototype” or “jquery”, which will handle all the specific issues transparently.

Or use these libs to determine the browser type if you really must.

Also see Browser & version in prototype library?

Method 6

For browser compatibility you can use this code. This method returns browser name and version :

private string GetBrowserNameWithVersion
{
    var userAgent = Request.UserAgent;
    var browserWithVersion = "";
    if (userAgent.IndexOf("Edge") > -1)
    {
        //Edge
        browserWithVersion = "Edge Browser Version : " + userAgent.Split(new string[] { "Edge/" }, StringSplitOptions.None)[1].Split('.')[0];
    }
    else if (userAgent.IndexOf("Chrome") > -1)
    {
        //Chrome
        browserWithVersion = "Chrome Browser Version : " + userAgent.Split(new string[] { "Chrome/" }, StringSplitOptions.None)[1].Split('.')[0];
    }
    else if (userAgent.IndexOf("Safari") > -1)
    {
        //Safari
        browserWithVersion = "Safari Browser Version : " + userAgent.Split(new string[] { "Safari/" }, StringSplitOptions.None)[1].Split('.')[0];
    }
    else if (userAgent.IndexOf("Firefox") > -1)
    {
        //Firefox
        browserWithVersion = "Firefox Browser Version : " + userAgent.Split(new string[] { "Firefox/" }, StringSplitOptions.None)[1].Split('.')[0];
    }
    else if (userAgent.IndexOf("rv") > -1)
    {
            //IE11
        browserWithVersion = "Internet Explorer Browser Version : " + userAgent.Split(new string[] { "rv:" }, StringSplitOptions.None)[1].Split('.')[0];
    }
    else if (userAgent.IndexOf("MSIE") > -1)
    {
        //IE6-10
        browserWithVersion = "Internet Explorer Browser  Version : " + userAgent.Split(new string[] { "MSIE" }, StringSplitOptions.None)[1].Split('.')[0];
    }
    else if (userAgent.IndexOf("Other") > -1)
    {
        //Other
        browserWithVersion = "Other Browser Version : " + userAgent.Split(new string[] { "Other" }, StringSplitOptions.None)[1].Split('.')[0];
    }

    return browserWithVersion;
}

Method 7

I tried and found the solution for the same

        public static string GetBrowserDetails()
        {
          string  BrowserDetails = HttpContext.Current.Request.Browser.Browser + " - " + HttpContext.Current.Request.Browser.Version + ";  Operating System : " + HttpContext.Current.Request.Browser.Platform;
          return BrowserDetails;
        }

OUTPUT : 
Chrome - 88.0; Operating System : WinNT

Method 8

use from

Request.Browser

this link will help you :

Detect the browser using ASP.NET and C#


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x