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. 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.
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.
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!!!