Friday, 29 November 2013

Detect Browser In Code Behind ASP.NET

Leave a Comment
Certain javascript will work on certain browser only. So inorder to avoid your client from using the application which is bot supported by your application, you can filter the browser type from code behind of your web application.
 
Let see the example

The ASPX page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>


The Code Behind

 protected void Page_Load(object sender, EventArgs e)
        {
            System.Web.HttpBrowserCapabilities browser = Request.Browser;
            string s = "Browser Capabilities &lt;br/&gt;"
                + "Type = " + browser.Type + "&lt;br/&gt;"
                + "Name = " + browser.Browser + "&lt;br/&gt;"
                + "Version = " + browser.Version + "&lt;br/&gt;"
                + "Major Version = " + browser.MajorVersion + "&lt;br/&gt;"
                + "Minor Version = " + browser.MinorVersion + "&lt;br/&gt;"
                + "Platform = " + browser.Platform + "&lt;br/&gt;"
                + "Is Beta = " + browser.Beta + "&lt;br/&gt;"
                + "Is Crawler = " + browser.Crawler + "&lt;br/&gt;"
                + "Is AOL = " + browser.AOL + "&lt;br/&gt;"
                + "Is Win16 = " + browser.Win16 + "&lt;br/&gt;"
                + "Is Win32 = " + browser.Win32 + "&lt;br/&gt;"
                + "Supports Frames = " + browser.Frames + "&lt;br/&gt;"
                + "Supports Tables = " + browser.Tables + "&lt;br/&gt;"
                + "Supports Cookies = " + browser.Cookies + "&lt;br/&gt;"
                + "Supports VBScript = " + browser.VBScript + "&lt;br/&gt;"
                + "Supports JavaScript = " +
                    browser.EcmaScriptVersion.ToString() + "&lt;br/&gt;"
                + "Supports Java Applets = " + browser.JavaApplets + "&lt;br/&gt;"
                + "Supports ActiveX Controls = " + browser.ActiveXControls
                      + "&lt;br/&gt;"
                + "Supports JavaScript Version = " +
                    browser["JavaScriptVersion"]  + "&lt;br/&gt;&lt;br/&gt;";

            

            Response.Write(s);

            switch (browser.Browser.ToUpper().Trim())
            {
                // WHY No OPERA BROWSER,because opera and chrome user agent is same, 
                // the browser.Browser will return Chrome value in Opera
                case "CHROME": Response.Write("<b>application support your browser</b>"); break;
                case "INTERNETEXPLORER": Response.Write("<b>application does not support your browser</b>"); break;
                case "FIREFOX": Response.Write("<b>application support your browser</b>"); break;
                case "SAFARI": Response.Write("<b>application support your browser</b>"); break;
                default: break;
            }
        } 

The Output


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.