IN THIS TASK
NOTE: This article assumes that you have ASP.NET and Visual Studio .NET installed on your computer.
Request.ServerVariables collection or use the new Request.UserAgent property to retrieve the HTTP_USER_AGENT header value. You can parse this string to determine the browser name, the major and minor versions of the browser, and if the browser is a beta release.
The following list includes two sample user agent strings:
- When you browse with Microsoft Internet Explorer 6.0 Beta, you may receive a user agent string similar to the following:User Agent :: Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
- When you browse with Internet Explorer 5.5, you may receive the following user agent string:User Agent :: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
Follow these steps to create a simple ASP.NET application using Visual Basic .NET that writes the browser name and versions back to the client browser in HTML:
- From the Start menu, point to Programs, point to Microsoft Visual Studio.NET, and click Microsoft Visual Studio.NET.
- Click New Project.
- In the New Project dialog box, under Project Type, click Visual Basic Projects. Under Templates, click ASP.NET Web Application. In the Name text box, type a name for the application.
- Double-click in the Design window of Webform1.
- Replace the code for the Page_Load event with the following code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Response.Write("<B>User Agent ::</B> " & Request.UserAgent & "<BR>")
Response.Write("<B>Browser ::</B> " & Request.Browser.Browser & "<BR>")
Response.Write("<B>Version ::</B> " & Request.Browser.Version & "<BR>")
Response.Write("<B>Major::</B> " & Request.Browser.MajorVersion() & "<BR>")
Response.Write("<B>Minor::</B> " & Request.Browser.MinorVersion() & "<BR>")
- Press the CTRL+S key combination to save the file.
- On the Build menu, click Build.
- After the build is complete, either click Start, or right-click Webform1.aspx in Solution Explorer, and then click View in Browser.
- In the browser window, notice that the User Agent string appears and includes the browser name, version, major versions, and minor versions.
Article ID: 306576 - Last Review: Mar 24, 2009 - Revision: 1