10

Here is my code below :

    User_Info.Add(!string.IsNullOrEmpty(Request.UserAgent) ? Request.UserAgent : string.Empty);//4:UserAgent

    HttpBrowserCapabilities browser = Request.Browser;
    User_Info.Add(!string.IsNullOrEmpty(browser.Browser) ?  "Name : " + browser.Browser + " | " +
                                                            "Type : " + browser.Type + " | " +
                                                            "MajorVersion  : " + browser.MajorVersion + " | " +
                                                            "MinorVersion  : " + browser.MinorVersion : string.Empty);//5:UserBrowser

What is the difference between Request.UserAgent and Request.Browser?
I couldn't understand those UserAgent strings!
Would you please show some examples with explanation?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
SilverLight
  • 19,668
  • 65
  • 192
  • 300

3 Answers3

14

UserAgent gives you a raw string about the browser. It might look like this:

User Agent :: Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)

Request.Browser will give you HttpBrowserCapabilities object which will have browser version information along with some extra information regarding the capabilities of the browser. For example:

  1. Whether browser supports Frames
  2. If it supports cookies ?
  3. Supports JavaScripts ?
  4. Supports Java Applets ? etc.

Look at the following sample code:

HttpBrowserCapabilities bc = Request.Browser;
Response.Write("<p>Browser Capabilities:</p>");
Response.Write("Type = " + bc.Type + "<br>");
Response.Write("Name = " + bc.Browser + "<br>");
Response.Write("Version = " + bc.Version + "<br>");
Response.Write("Major Version = " + bc.MajorVersion + "<br>");
Response.Write("Minor Version = " + bc.MinorVersion + "<br>");
Response.Write("Platform = " + bc.Platform + "<br>");
Response.Write("Is Beta = " + bc.Beta + "<br>");
Response.Write("Is Crawler = " + bc.Crawler + "<br>");
Response.Write("Is AOL = " + bc.AOL + "<br>");
Response.Write("Is Win16 = " + bc.Win16 + "<br>");
Response.Write("Is Win32 = " + bc.Win32 + "<br>");
Response.Write("Supports Frames = " + bc.Frames + "<br>");
Response.Write("Supports Tables = " + bc.Tables + "<br>");
Response.Write("Supports Cookies = " + bc.Cookies + "<br>");
Response.Write("Supports VB Script = " + bc.VBScript + "<br>");
Response.Write("Supports JavaScript = " + bc.JavaScript + "<br>");
Response.Write("Supports Java Applets = " + bc.JavaApplets + "<br>");
Response.Write("Supports ActiveX Controls = " + bc.ActiveXControls + "<br>");
Response.Write("CDF = " + bc.CDF + "<br>");

For comparing browser version against a user agent, you would have to use string operations (Contains), whereas in case of Request.Browser you can compare against a property.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Habib
  • 219,104
  • 29
  • 407
  • 436
  • I'm wondering if the HttpBrowserCapabilities class is still relevant. I put this code in a Controller along with many of the other properties it exposes, then went to the page in Chrome, Edge, Safari (for windows), and Firefox. Much of the information returned, such as ScreenPixelsHeight = 480 and ScreenPixelsWidth = 640 was the same and also incorrect. – MikeT Feb 27 '16 at 15:10
8

Request.Browser is different from Request.UserAgent. UserAgent gets the raw user agent string of the client browser, and Request.Browser gets you the information about the browser capabilities. You wont get all the browser capabilities with the UserAgent string.

Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
1

Request.UserAgent is a bit cryptic, and requires parsing to determine what browser, specifically, a visitor is using. Furthermore, it doesn't contain information like what version of JavaScript the browser supports, or if the browser supports CSS 2.0 stylesheets

The Request.Browser property is an instance of the HttpBrowserCapabilities object provides all the information...

Ref: https://web.archive.org/web/20211020150659/https://www.4guysfromrolla.com/articles/120402-1.aspx

arun.v1
  • 462
  • 1
  • 4
  • 12