jump to navigation

Retrieve browser information from an asp.net(vb) web application June 24, 2008

Posted by fofo in asp.net, asp.net 2.0, VB 2005, Visual Studio 2008.
Tags:
trackback

In a recent project of mine, I had to collect information on what are the browser capabilities of a given user who would access through that browser 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 a asp.net web application.

So we must launch visual studio and then create a asp.net web application. I am going to use VB.

When we create a new web application,we have the Default.aspx page.

we go to our code-behind page, so we are in the the Default.aspx.vb page and in the Page Load event we can write the following code

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.JavaScript.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 IE7.

all the rest are straightforward. run the web application and you will see all the information. Here is how it looks when i run my application.

general browser family : IE
browser type : IE7
supports javascript : True
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.

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to Ma.gnoliaAdd to TechnoratiAdd to Furl

Comments»

1. Eprofit Academy Reviews - March 12, 2013

I blog often and I truly appreciate your information.

This article has really peaked my interest. I’m going to bookmark your site and keep checking for new information about once per week. I opted in for your RSS feed as well.

2. Johne600 - May 4, 2014

I think this is a real great blog post.Much thanks again. bgdaafekegga


Leave a comment