53

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?

Linus Caldwell
  • 10,908
  • 12
  • 46
  • 58
cnd
  • 32,616
  • 62
  • 183
  • 313
  • 9
    **Don't browser sniff** and especially don't browser sniff on the server side. You are setting yourself up for breakage with proxy caches unless you include the proper `Vary: User-Agent` header, in which case you are breaking caching in IE. Find a client-side way of doing what you're doing (eg. IE's conditional comments), and prefer to sniff capabilities instead of just the unreliable user-agent name. What is the browser difficulty you are trying to work around with this sniff? – bobince Feb 08 '10 at 13:46
  • I've got a jQuery script only for FF and IE Engines – cnd Feb 09 '10 at 05:37

8 Answers8

79
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 { }
lkaradashkov
  • 8,609
  • 1
  • 15
  • 12
Asad
  • 21,468
  • 17
  • 69
  • 94
27

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 Capabilities\n"
        + "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

ebram khalil
  • 8,252
  • 7
  • 42
  • 60
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • 6
    Tony-The-Lion: I did not insult you, just stating the facts, and here's the proof. That code does not tell whether you are running Firefox or Chrome. It is only good for IE. The output from the code above is:\n\n IE 9: Type = IE9 Browser = IE FIREFOX 17: Type = Mozilla17 Browser = Mozilla CHROME 23: Type = Desktop Browser = AppleMAC-Safari. Run that code for yourself and see what it will output. I cannot paste the entire output because this edit field allows only a limited number of characters. – scrat.squirrel Nov 29 '12 at 16:49
10

Try the below code

HttpRequest req = System.Web.HttpContext.Current.Request
string browserName = req.Browser.Browser;
Rahul Mahadik
  • 794
  • 11
  • 19
1
    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;
    }
0

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?

Community
  • 1
  • 1
ron
  • 9,262
  • 4
  • 40
  • 73
  • why hacking ... ? I just got J code for FF Engine and IE Engine and must to make other stuff for other browsers. – cnd Feb 08 '10 at 13:24
  • also, not everything that you might need to detect browser will be "fixed" by using jquery / prototype... for example, I have a site that previews files in a repository. MSIE is the only browser that supports previewing XPS files - that's not something that jquery or prototype can fix. – Michael Bray Jun 30 '12 at 00:07
0

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;
}
Nahid
  • 336
  • 1
  • 12
0

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
Anto sujesh
  • 325
  • 3
  • 14
-3

use from

Request.Browser

this link will help you :

Detect the browser using ASP.NET and C#

masoud ramezani
  • 22,228
  • 29
  • 98
  • 151