Creating an Installation Page That Automatically Detects the .NET Framework Version

 

Barak Cohen
Microsoft Corporation

August 2005

Summary: This white paper shows how one can create an application installation page that uses the userAgent string to detect which .NET Framework components are installed on a computer. (5 printed pages)

Introduction

One of the useful properties of the .NET Framework is that it marks itself on the browser header information. The information is thus easily obtainable and can be used to smooth the experience of application installations that rely on these software components.

How the userAgent String Is Used

The user agent string includes formatted information about the configuration of the computer that hits the page. The information can be detected locally (using JavaScript) or remotely (by a server script). The information looks like this:

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 
1.1.4322; .NET CLR 1.0.3705)

As you can see, the information includes the operating system, the browser type, the locale, and the version of the CLR that is installed.

Identifying the Runtime from a Local Hosted Script (HTML)

The following code illustrates how an installation page can use script to determine whether the runtime is needed. It also provides a link to download.

<HTML>
  <HEAD>
    <TITLE>Application Sample Installation Page</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8" />

   <meta name="description" content="A sampe page that shows how to autodetect the .NET Framework for an installation">

<SCRIPT LANGUAGE="JavaScript">
<!--
CLRVersion = "1.1.4322";

function window::onload()
{   
   dnltxt="\n <a href=\"https://download.microsoft.com/download/a/a/c/aac39226-8825-44ce-
90e3-bf8203e74006/dotnetfx.exe\">Download it now.</a>";
    
    if(HasRuntimeVersion(CLRVersion))
    {
        result.innerText = "This computer has the correct version of CLR: " + CLRVersion + "."
        + " This computer's userAgent string is: " + navigator.userAgent + ".";

    }
    else
    {
        result.innerText = "This computer does not have the correct 
version of CLR: " + CLRVersion + "."
        + " This computer's userAgent string is: " + 
navigator.userAgent + ".";  
    }  
}
function HasRuntimeVersion(v)
{
  var va = GetVersion(v);
  var i;
  var a = navigator.userAgent.match(/\.NET CLR [0-9.]+/g);
  if (a != null)
    for (i = 0; i < a.length; ++i)
      if (CompareVersions(va, GetVersion(a[i])) <= 0)
      return true;
  return false;
}
function GetVersion(v)
{
  var a = v.match(/([0-9]+)\.([0-9]+)\.([0-9]+)/i);
    return a.slice(1);
}
function CompareVersions(v1, v2)
{
  for (i = 0; i < v1.length; ++i)
  {
    var n1 = new Number(v1[i]);
    var n2 = new Number(v2[i]);
    if (n1 < n2)
      return -1;
    if (n1 > n2)
      return 1;
  }
  return 0;
}

-->
</SCRIPT>

</HEAD>
  <BODY bgcolor="#FFFF99">
  <h1>Application Sample Installation Page</h1>
   <p>This page auto detects if your computer has the correct 
version of the .NET Framework.</p>

    <div id="result" /> </div>
  
    <script>
    if(!HasRuntimeVersion(CLRVersion))
    {
        document.write("<a href=\"https://download.microsoft.com/download/a/a/c/aac39226-8825-44ce-
90e3-bf8203e74006/dotnetfx.exe\">Download it now.</a>");
    }
    </script>
 
  <p><b>Please download and install the 
   <a href="https://msdn.microsoft.com/netframework/">application</a></b>.</p>
  </BODY>
</HTML>

The resulting page will look like this (on a computer without the .NET Framework):

Application Sample Installation Page

This page auto detects if your computer has the correct version of the .NET Framework.

This computer has the correct version of CLR: 1.1.4322. This computer's userAgent string is: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322).

Please download and install the application

The same page will render differently on a computer without the correct version:

Application Sample Installation Page

This page auto detects if your computer has the correct version of the .NET Framework.

This computer does not have the correct version of CLR: 1.1.34322. This computer's userAgent string is: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1).

Download it now.

Please download and install the application

Identifying the Runtime from a Server-Hosted Script (ASPX)

In a similar way, a server application (ASP.NET) can act upon the parsing of the user agent information. This ASPX sample uses panes that are exposed based on the existence of the .NET Framework.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Configuration" %>

<HTML>
<HEAD>
<script runat="server">
void Page_Load(Object sender, EventArgs e) {
        Version clrVersion = Request.Browser.ClrVersion;
        if (clrVersion.Major > 0)
            aOkPanel.Visible = true;
        else
            downloadPanel.Visible = true;
    }
void Download_Command(Object sender, CommandEventArgs e) {
    //NOTE: Update this URL to link to the version of the .NET 
Framework you would like the user to install
Response.Redirect("https://www.microsoft.com/downloads/details.aspx?Fami
lyId=262D25E3-F589-4842-8157-034D1E7CF3A3&displaylang=en");
}
</script>
</HEAD>

<BODY> 
<FORM runat="server" ID="Form1">
<SPAN class="Head">Checking for .NET Framework...</SPAN>
<asp:Panel ID="aOkPanel" Visible="False" CssClass="Normal" Runat="server">
<P>
<SPAN style="FONT-SIZE: 24pt; COLOR: #6699ff; FONT-FAMILY: 
Verdana">.NET Framework Found</SPAN><BR>
<BR>
The correct version of the .NET Framework is already installed on your 
computer.&nbsp; You are ready to install the application.
<BR>
</asp:Panel>
<asp:Panel ID="downloadPanel" Visible="False" CssClass="Normal" Runat="server">
<P>
<SPAN style="FONT-SIZE: 24pt; COLOR: #6699ff; FONT-FAMILY: 
Verdana">.NET Framework Not Found</SPAN><BR>
<BR>
The application requires Version 1.1&nbsp;or later of the .NET 
Framework that is not currently installed on your computer.</P>
Please download the .NET Framework Redistributable before installing the application. 
<BR>
<asp:LinkButton id="LinkButton1" runat="server" CssClass="SubHead" 
Text="Download .NET Framework Redistributable Now" 
CommandName="V11REDIST" OnCommand="Download_Command"></asp:LinkButton> (23 MB)
<BR>
</asp:Panel>
</FORM>
</BODY>
</HTML>

Conclusion

By using the user agent information, ISVs can create an easy and convenient installation experience for applications that rely on certain runtime components that are discoverable from the browser user agent. This can be done using client-side script or by using server-side script that parses the information to create the desired experience.