.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to detect browser and its other properties in ASP.Net

Posted By:ASPEvil       Posted Date: June 28, 2014    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

How to detect browser name and its other properties, like: its version, type etc; in ASP.Net.
 

Hello,

 In some cases, our ASP.Net websites should know exactly which browser the users are using to view our websites and a bit more information about that particular browser, like: its version, type etc. In this article, I am going to show you how we can get these information.

All the browser related information are in the "Request.Browser".

So if we want to know the browser name, then we can use the following code:-
--------------------------
VB.Net code:
Dim browserName As String = Request.Browser.Browser
C# code:
string browserName = Request.Browser.Browser;

Now the browser name is with our string object: "browserName". You can now use this string with your code and do some action as per the requirement. I provided an example below:-

Vb.Net code:
If browserName = "Chrome" Then
          
            'Your Code goes here...

        ElseIf browserName = "Firefox" Then
            'Your Code goes here...

        ElseIf browserName = "IE" Then
                     
                'Your Code goes here...
            
        End If


C# code:

if (browserName == "Chrome") {

	//Your Code goes here...

} else if (browserName == "Firefox") {
	//Your Code goes here...

} else if (browserName == "IE") {

	//Your Code goes here...

}

Now lets consider a situation where after getting the browser name, you want to know the version of that same browser.
For example: you are wanting to know if the user is using Internet Explorer and if yes, then whether the user is using Internet Explorer 8.0 Or Internet Explorer 9.0 ?

The following code do this:-

Vb.Net code:

Dim browserName As String = Request.Browser.Browser

        Dim browserVersion As String = Request.Browser.Version
 

        If browserName = "IE" Then
            If browserVersion = "8.0" Or browserVersion = "9.0" Then                
                'Your Code goes here...
            End If
        End If
 C# code:

string browserName = Request.Browser.Browser;

string browserVersion = Request.Browser.Version;


if (browserName == "IE") {
	if (browserVersion == "8.0" | browserVersion == "9.0") {
		//Your Code goes here...
	}
}


We can not only get the browser name and its version information, but also we can get several more information of the browsers, as follow:-
---------------------------------
MajorVersion
MinorVersion
CanSendMail

etc.

To see a detailed list, just Type: "Dim info As String=Request.Browser." with vb.net and "string info = Request.Browser." with C# and just after entering the last dot(.), the Visual Studio will show you a detailed list of all the available information we can get for a browser.

Please Note: with the above lines: "Dim info As String=Request.Browser." and "string info = Request.Browser."; "info" is a string object name, I used, but you can use any name, you like, for this same string object.



Thanks.
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend