Reputation: 20468
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?
Upvotes: 11
Views: 33375
Reputation: 223402
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:
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.
Upvotes: 14
Reputation: 472
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
Upvotes: 1
Reputation: 13150
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.
Upvotes: 8