Get User Agent

Get User Agent

User Agents

  • A text sequence that a browser reports to a web site that helps web sites provide layouts for the device
  • Contains the brand, version, plugins, and toolbars installed on the browser
  • ASP.NET provides a HttpBrowserCapabilities class that parses the User-Agent HTTP header

Database of User Agents

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Data.Items.Item
  • System.Web.HttpContext
  • System.Web.HttpRequest
  • System.State

User Agent C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item; 
if (item == null) return; 

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both"); 
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div); 
output.Write("C# Web Control Real-time Output"); 
output.RenderEndTag(); //Div 

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:small; width:100%; clear:both"); 
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div); 
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold"); 
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P); 
//using : for colon and   for space 
output.Write("{0}: {1}", "Item", item.DisplayName); 
output.RenderEndTag(); //P 

//System.Web.HttpApplication httpApp = new System.Web.HttpApplication(); 
System.Web.HttpRequest request = System.Web.HttpContext.Current.Request; 
string userAgent = request.UserAgent; 
if (!System.String.IsNullOrEmpty(userAgent)) 
output.Write("{0}&#58;&#160;{1}", "Your browser is reporting the following user agent text:<br/>", "<b>" + userAgent + "</b>"); 
else 
output.Write("{0}", "User Agent string not found."); 

output.RenderEndTag(); //Div

 

User Agent C# Web Control Output

Your browser is reporting the following user agent text:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36



Sources