KnowDotNet

Use HTTPRequest To Create a Web Browser Business Object

Return Browser Information with HTTPRequest Object

by Les Smith

Create a Web Browser Business Object to return information about the Web Browser being used to accept your Web Site.  Not all Web Browsers have the same capability.  For example, if you are used to using Internet Explorer, as we normally do at the company for which I work, we often run into problems with some of the older or new Browsers.

In logging exceptions thrown by a Web Site, you may need to record information regarding the Web Browser that is being used.  Sometimes the application may have a bug, sometimes the user does something you were not expecting, and sometimes the Web Browser that the user is using does not support some functionality that you have in your site.  Therefore you should record information regarding the current browser being used.  

This article shows how to create a Business Object that presents the information about the browser.  This information is available in the HTTPRequest object.  The following C# code is the code for the Business Object.  Note, that the constructor does nothing but save the HTTPRequest object.  Then the individual properties return the desired value using the reques object stored by the constructor.  In another article I will show a shortcut for getting this information in the exception logging DLL, but I had already written this Business Object, so it may be useful for other purposes.

using System.Text;
using System.Web;

namespace BusinessObjects
{
    
public class ExceptionLog
    {
        #region private members
        
private HttpRequest _request;

        #endregion // private members

        #region public members
        
public string UserHostAddress
        {
            
get { return _request.UserHostAddress; }
        }
        
public string BrowserType
        {
            
get { return _request.Browser.Type; }
        }
        
public string BrowserName
        {
            
get { return _request.Browser.Browser; }
        }
        
public string BrowserVersion
        {
            
get { return _request.Browser.Version; }
        }
        
public string BrowserMajorVersion
        {
            
get { return _request.Browser.MajorVersion.ToString(); }
        }
        
public string BrowserMinorVersion
        {
            
get { return _request.Browser.MinorVersion.ToString(); }
        }
        
public string Platform
        {
            
get { return _request.Browser.Platform; }
        }
        
public bool IsBeta
        {
            
get { return _request.Browser.Beta; }
        }
        
public bool IsCrawler
        {
            
get { return _request.Browser.Crawler; }
        }
        
public bool IsAOL
        {
            
get { return _request.Browser.AOL; }
        }
        
public bool IsWin16
        {
            
get { return _request.Browser.Win16; }
        }
        
public bool IsWin32
        {
            
get { return _request.Browser.Win32; }
        }
        
public bool SupportsFrames
        {
            
get { return _request.Browser.Frames; }
        }
        
public bool SupportsTables
        {
            
get { return _request.Browser.Tables; }
        }
        
public bool SupportsCookies
        {
            
get { return _request.Browser.Cookies; }
        }
        
public bool SupportsVBScript
        {
            
get { return _request.Browser.VBScript; }
        }
        
public bool SupportsJavaScript
        {
            
get { return _request.Browser.JavaScript; }
        }
        
public bool SupportsJavaApplets
        {
            
get { return _request.Browser.JavaApplets; }
        }
        
public bool SupportsActiveXControls
        {
            
get { return _request.Browser.ActiveXControls; }
        }


        #endregion // public members

        #region constructor

        
public ExceptionLog(HttpRequest request)
        {
            _request = request;

        }
// constructor

        #endregion // constructor
    }
}

Code to create this Business Object and pass it to the exception logger would be as follows:

   string errorCode = ExceptionHelper.LogException(ex,
                      
Environment.MachineName,
                      UserId,
                      
SessionWrapper.Current.ApplicationId,
                      
new ExceptionLog(Request),
                      
ApplicationWrapper.DBConnection);

I am not showing the ExceptionHelper class since that is a function of your database table structure and you can write that to suit your database needs.

Need to automatically organize your code windows?  You'll be amazed how easy it is to keep the code in your code windows organized.  TRY IT FREE FOR 30 DAYS BY CLICKING HERE.



Automatically generate braces in C#! Try CSharpCompleter and stop wasting valuable time needlessly typing hundreds of braces {} daily.  Try CSharpCompleter for 30 DAYS FREE.



Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog.
  

You can also email me directly at les@KnowDotNet.com.