Recipe 21.5. Determining the User s Browser Type


Recipe 21.5. Determining the User's Browser Type

Problem

Your application requires the use of a specific browser and you want to determine if the required browser is being used before allowing a user to access your application.

Solution

Use the properties of the Request.Browser object to determine the browser type and version, and take the action required by your application.

In the code-behind class for the page, use the .NET language of your choice to:

  1. Use the Browser property of the Request.Browser object to return a string representing the full browser type, such as IE in the case of Internet Explorer.

  2. Use the Version property of the Request.Browser object to return a string that represents the major and minor version of the browser, such as 6.0 in the case of IE 6.0.

  3. Take action accordingly, such as outputting a message indicating whether the user's browser is compatible with the application.

Examples 21-10, 21-11 through 21-12 show the .aspx file and the VB and C# code-behind files for an application that demonstrates the solution. The output of our example program is shown in Figure 21-2.

Figure 21-2. Determining the user's browser


Discussion

With different browsers in use today and the significant variation in their capabilities, determining the type of browser being used is common. Based on the browser, you may need to inform the user that the browser is incompatible with your application or output different HTML to support the specific browser.

Our example demonstrates the functionality provided in ASP.NET to determine the browser type and version, and then outputs this information along with a message indicating if the user's browser is compatible with the application. The .aspx file contains two asp:Literal controls used to output messages to the user. The first outputs the browser version information, and the second informs the user if his browser is compatible with the application.

In the Page_Load method of the code-behind, the Browser and Version properties of the Request.Browser object are used to create a message to inform the user of the detected browser version. The Browser property returns a string such as IE, Firefox, or Opera. The Version property returns a string that represents the major and minor version of the browser. For Internet Explorer 6.0, for instance, the Browser property will return IE, and the Version property will return 6.0.

Next, the browser type and major version number are compared to the minimum requirements for the application (IE and Version 5). If the browser is IE 5.0 or later, a message is displayed indicating the browser is compatible with the application. Otherwise, a message is displayed indicating the application requires IE 5.0 or later.

For your application, you might want to check the browser version on the home page. If the browser is compatible with your application, output the home page normally. Otherwise, redirect the user to a message page indicating the browser requirements.

The Request.Browser object contains many properties that are not used in this example but may be useful in your application. Table 21-1 lists some of the commonly used properties. For a complete list of the available properties, refer to the documentation on the HttpBrowserCapabilities class in the MSDN Library.

Table 21-1. Commonly used browser object properties

Property

Description

Browser

Returns a string indicating the browser type (IE, Firefox, Opera, etc.)

Cookies

Returns a Boolean value indicating if the browser supports cookies

JavaScript

Returns a Boolean value indicating if the browser supports JavaScript

MajorVersion

Returns an integer value indicating the major version of the browser (integer portion of the browser version)

MinorVersion

Returns a double value indicating the minor version of the browser (decimal portion of the browser version)

Version

Returns a string representing the full browser version (integer and decimal portion)


Properties that return Boolean values, such as Cookies and JavaScript, indicate what the browser is capable of supporting but not necessarily the current configuration. If the browser supports cookies but the user has configured the browser to disable cookies, the Cookies property will still return true.


See Also

HttpBrowserCapabilities documentation in the MSDN Library

Example 21-10. Determining the user browser type (.aspx)

 <%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master" AutoEventWireup="false" CodeFile="CH21DeterminingBrowserVB.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH21DeterminingBrowserVB" Title="Determining Browser" %> <asp:Content  runat="server" ContentPlaceHolder> <div align="center" > Determining User Browser (VB) </div> <br /> <table width="90%" align="center" border="0"> <tr> <td align="center" > <asp:Literal  Runat="server" /> <br /><br /> <asp:Literal  Runat="server" /> </td> </tr> </table> </asp:Content> 

Example 21-11. Determining the user browser type code-behind (.vb)

 Option Explicit On Option Strict On Namespace ASPNetCookbook.VBExamples ''' <summary> ''' This class provides the code-behind for ''' CH21DeterminingBrowserVB.aspx ''' </summary> Partial Class CH21DeterminingBrowserVB Inherits System.Web.UI.Page '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the page load event. It ''' is responsible for initializing the controls on the page. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Private Sub Page_Load(ByVal sender As Object, _   ByVal e As System.EventArgs) Handles Me.Load 'output user browser litBrowser.Text = "Your browser is " & _ Request.Browser.Browser & " " & _ Request.Browser.Version 'check to see if it is an acceptable version If ((Request.Browser.Browser.Equals("IE")) AndAlso _ (Request.Browser.MajorVersion >= 5)) Then   'output message indicating it is OK   litMessage.Text = "It is compatible with this application." Else   'output message indicating IE 5 or later must be used   litMessage.Text = "This application requires IE 5.0 or later." End If End Sub 'Page_Load  End Class 'CH21DeterminingBrowserVB  End Namespace 

Example 21-12. Determining the user browser type code-behind (.cs)

 using System; namespace ASPNetCookbook.CSExamples { /// <summary> /// This class provides the code-behind for  /// CH21DeterminingBrowserCS.aspx  /// </summary> public partial class CH21DeterminingBrowserCS : System.Web.UI.Page  { ///*********************************************************************** /// <summary> /// This routine provides the event handler for the page load event. /// It is responsible for initializing the controls on the page. /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void Page_Load(object sender, EventArgs e) { // output user browser litBrowser.Text = "Your browser is " +   Request.Browser.Browser + " " +   Request.Browser.Version; // check to see if it is an acceptable version if ((Request.Browser.Browser.Equals("IE")) & (Request.Browser.MajorVersion >= 5)) { // output message indicating it is OK litMessage.Text = "It is compatible with this application."; } else { // output message indicating IE 5 or later must be used litMessage.Text = "This application requires IE 5.0 or later."; } } // Page_Load  } // CH21DeterminingBrowserCS  } 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net