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?

2
  • 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? Commented Feb 8, 2010 at 13:46
  • I've got a jQuery script only for FF and IE Engines Commented Feb 9, 2010 at 5:37

8 Answers 8

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 { } 
Sign up to request clarification or add additional context in comments.

5 Comments

there is no such parameter "Name" and ... IE and FF is Internet Explorer and Fire Fox ... I don't think it's FF :-S
doesn't works for mozilla... tried "Mozilla" instead "FF" but that didn't helped me too.
This is terribly broken as of today. Request.Browser returns Mozilla17 for Firefox 17. For Google Chrome 23 it returns AppleMAC-Safari. What a holy mess!
You may want to to use request.UserAgent
The Type is "InternetExplorer11" for IE11.
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

1 Comment

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

Try the below code

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

Comments

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; } 

Comments

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?

2 Comments

why hacking ... ? I just got J code for FF Engine and IE Engine and must to make other stuff for other browsers.
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.
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; } 

Comments

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 

Comments

-3

use from

Request.Browser 

this link will help you :

Detect the browser using ASP.NET and C#

1 Comment

this doesn't help, the link provided contains code copied from MSDN, which is not helpful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.