Thursday 25 October 2007

Detect browser in asp.net C#

Detect browser in asp.net

This article will give a little information on detecting the browser in asp.net. We can use HttpBrowserCapabilities class with C# language. HttpBrowserCapabilities class gives information on the capabilities of the browser that is running on the client. It needs the System.Web namespace. HttpBrowserCapabilities properties are accessible through the Browser property of ASP.NET's intrinsic Request object.

I have mention here the HttpBrowserCapabilities members:

An example in C# to get browser detection

<script language="C#" runat="server">
  Protected void Page_Load(sender as Object, e as EventArgs)
{
    ltlBrowserName.Text = Request.Browser.Type + ", " + Request.Browser.Platform
    ltlAllData.Text = "Type = " + Request.Browser.Type + "<br>"
    ltlAllData.Text += "Name = " + Request.Browser.Browser + "<br>"
    ltlAllData.Text += "Version = " + Request.Browser.Version + "<br>"
    ltlAllData.Text += "Major Version = " + Request.Browser.MajorVersion + "<br>"
    ltlAllData.Text += "Minor Version = " + Request.Browser.MinorVersion + "<br>"
    ltlAllData.Text += "Platform = " + Request.Browser.Platform + "<br>"
    ltlAllData.Text += "Is Beta = " + Request.Browser.Beta + "<br>"
    ltlAllData.Text += "Is Crawler = " + Request.Browser.Crawler + "<br>"
    ltlAllData.Text += "Is AOL = " + Request.Browser.AOL + "<br>"
    ltlAllData.Text += "Is Win16 = " + Request.Browser.Win16 + "<br>"
    ltlAllData.Text += "Is Win32 = " + Request.Browser.Win32 + "<br>"
    ltlAllData.Text += "Supports Frames = " + Request.Browser.Frames + "<br>"
    ltlAllData.Text += "Supports Tables = " + Request.Browser.Tables + "<br>"
    ltlAllData.Text += "Supports Cookies = " + Request.Browser.Cookies & "<br>"
    ltlAllData.Text += "Supports VB Script = " + Request.Browser.VBScript + "<br>"
    ltlAllData.Text += "Supports JavaScript = " + Request.Browser.JavaScript + "<br>"
    ltlAllData.Text += "Supports Java Applets = " + Request.Browser.JavaApplets + "<br>"
    ltlAllData.Text += "CDF = " + Request.Browser.CDF + "<br>"      
  }
</script>
 
Your browser is: <asp:literal id="ltlBrowserName" runat="server" />
<p>
<b><u>Here is your browser's information:</u></b><br />
<asp:literal runat="server" id="ltlAllData" />