Web Browser Name in C#

It is very important to track every visitor of a Web site or Web applications. During tracking some common elements are visitors IP address, MAC address, OS, Web browser details, etc. This article explains how to get browser details ASP.NET C#? Summary of the article:

  • What is Web Browser?
  • Why we should collect Web Browser Name?
  • How to Get the Browser Name in C#?
  • How to Get the Browser Version in C#?
  • How to Get the Operating System or OS Name in C#?
  • C# Method to Get the Browser Name

What is Web Browser?
A web browser or browser is a software application used to retrieving, presenting and traversing information resources on the WWW or World Wide Web. All information resource is identified by a Uniform Resource Identifier (URI/URL) and may be a web page, image, sound, and video. In client/server model, the browser is the client run on a computer that contacts the Web server and requests information. After processing the Web server sends the information back to the Web browser which displays the results.

The first web browser was invented by Sir Tim Berners-Lee in 1990. Its name was “WorldWideWeb” and later renamed Nexus. Currently the major web browsers are Mozilla Firefox, Google Chrome, Internet Explorer, Opera, Safari, etc.

Why we should collect Web Browser Name?
In our project sometimes we need to keep the browsing log of a user or Clint, users activities. During that time we store the visitors browsing date time, IP address, Web browser name, OS etc. This is required for security purpose. Later we can analyze them for different purpose. Logs can help us to check the status of a user.

How to Get the Browser Name in C#?
By using a simple C# code we can get the web browser name easily. A sample C# code is given bellow:

string WebBrowserName = string.Empty;
WebBrowserName = HttpContext.Current.Request.Browser.Browser;

How to Get the Browser Version in C#?
We can identify the Web browser version details by using C# language. A sample C# code that detect browser versions is given bellow:

string WebBrowserVersion = string.Empty;
WebBrowserVersion = HttpContext.Current.Request.Browser.Version;

How to Get the Operating System or OS Name in C#?
We can get the operating system (OS) name of the visitors of a site by using C# language. A sample  C# code to find OS name is given bellow:

string OSName = string.Empty;
OSName = HttpContext.Current.Request.Browser.Platform;

C# Method to Get the Browser Name
We can also write a method that will detect the browser details. This method can be reused int different classes.¬† A sample C# codes of a method “GetWebBrowserName()” is given bellow:

public string GetWebBrowserName()
{
    string WebBrowserName = string.Empty;
    try
    {
        WebBrowserName = HttpContext.Current.Request.Browser.Browser + " " + HttpContext.Current.Request.Browser.Version;
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
    return WebBrowserName;
}

For example: If we call the method GetWebBrowserName() and the clients browser name is Firefox it will returns “Firefox 28.0”.

For any smart application it commendatory to keep user logs. It will increase the efficiency of the applications. The next time it will open new possibilities. That’s why try to keep logs as much as u can, if it is not required for current time.

Comments

  1. By Ejaz

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *