Thursday, October 25, 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" />

 

4 comments:

  1. Ashish Kumar RaiJuly 28, 2008 at 5:08 PM

    Nice One
    I am looking for this.
    Thanks
    @shish Rai

    ReplyDelete
  2. C# won't compile this.

    ReplyDelete
  3. I am not getting correct value using Request.Browser.JavaScript ,i have tried this by disabling java script in my browser still its returning true

    ReplyDelete
  4. hi,

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I

    was with the information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful

    for .Net community.

    There is a good C# resource site, Have alook

    http://www.csharptalk.com/2009/09/c-class.html
    http://www.csharptalk.com

    simi

    ReplyDelete

Comments posted on ASP.Net Ajax Tutorials Blog are moderated and will be approved only if they are on-topic and not abusive. Please email me or my team for tech-support or blogging related questions. Avoid including website URLs in your comments - Thanks Author