Nikolaos Kantzelis ASP.Net Blog

This blog will focus on ASP.NET Framework

Sponsors

About Me

Great Blogs

How to obtain browser information from an asp.net application

In this post I am going to explain how to get information about the user's browser. This is the browser that the user uses to access the web application.

You can accomplish what i am going to show you with javascript. But I approached it differently and i will show you how to do that with .Net code.

I will be using VS 2010 Ultimate edition and VB.Net to create a simple asp.net application.


1) Launch Visual Studio 2010/2008/2005.Express edition will suffice.

2) Create a web site with an appropriate name. Use VB.Net as the development language.

3) In the Page_Load event handling routine type

Dim myusersbrowser As HttpBrowserCapabilities = Request.Browser

 Response.Write("general browser family" + " : " + _
                       myusersbrowser.Browser)
        Response.Write("</br>")
        Response.Write("browser type" + " : " + myusersbrowser.Type)
        Response.Write("</br>")
        Response.Write("supports javascript" + " : " + _
                       myusersbrowser.EcmaScriptVersion.ToString())
        Response.Write("</br>")
        Response.Write("javascript version" + " : " + _
                       myusersbrowser.JScriptVersion.ToString())
        Response.Write("</br>")
        Response.Write("does the browser support cookies?" + " : " + _
                       myusersbrowser.Cookies.ToString())
        Response.Write("</br>")
        Response.Write("search engine web crawler?" + " : " + _
                       myusersbrowser.Crawler.ToString())
        Response.Write("</br>")
        Response.Write("is this a mobile browser?" + " : " + _
                       myusersbrowser.IsMobileDevice.ToString())
        Response.Write("</br>")
        Response.Write("Active X controls enabled?" + " : " + _
                       myusersbrowser.ActiveXControls.ToString())
        Response.Write("</br>")
        Response.Write("Platform" + " : " + myusersbrowser.Platform)

I will try to explain what happens here. It is really simple.

We first have to create an instance(object) of the HttpBrowserCapabilities class. This class lives in the System.Web namespace.

In order to get an instance of this class we need to get hold of the Browser property of the Request object.

Dim myusersbrowser As HttpBrowserCapabilities = Request.Browser

then I use Response object and the Write method to write the information to the page using the various properties of the browser object I just created.

Response.Write(“browser type” + ” : ” + myusersbrowser.Type)


The line above gives us the Browser type, in my case IE8.

4) Run the web application and you will see all the information. In my case it is

general browser family : IE
browser type : IE8
supports javascript : 1.2
javascript version : 5.6
does the browser support cookies? : True
search engine web crawler? : False
is this a mobile browser? : False
Active X controls enabled? : True
Platform : WinNT


Please note that you can experiment with dozens of other properties of the  browser object.

This information can become very handy if ,for example, we have an Ajax enabled website but we still care for those people who have Javascript disabled.

We can use portion of the code above to redirect our users accordingly. Those who have Javascript disabled they are redirected to our simple non-Ajax website.

Hope it helps!!!

Posted: Sep 23 2010, 07:13 PM by nikolaosk | with 3 comment(s) |
Filed under: , ,

Comments

Vasilisf said:

Very userfull post . Thank you very much

# September 23, 2010 6:48 PM

Joe said:

this stuff is really great...:) keep up the good work

# September 25, 2010 12:01 PM

Maunank Shah said:

Hi...Thanks you so much ...

I am really thankful to yo for such a help...

# April 24, 2012 1:28 PM