π Introduction
Sometimes you want to display how long your web server has been running. In the early days, you might think of launching a background process at system startup to record the time and then calculate the difference in your ASP code. But there is a more elegant and dynamic solution using ASP.NET and AJAX together. This article explains both approaches and shows you how to implement a modern version.
π§ͺ The Old-School Approach
Before diving into ASP.NET, letβs look at the original workaround used to calculate server uptime:
A small executable runs once at system startup.
This app writes the current date and time to a file and exits.
The ASP page reads that file.
It subtracts the recorded startup time from the current time and displays the difference.
Although functional, this method requires file system access and startup configuration, which may not be ideal in all hosting environments.
π A Smarter Way Using ASP.NET and AJAX
You can leverage the power of the System.Diagnostics
namespace in ASP.NET to get the server's uptime directly using a performance counter. Then, you can display this information inside your classic ASP page using AJAX.
π οΈ Step 1: Create an ASPX Page (uptime.aspx
)
Save the following code as uptime.aspx
in your web directory:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Diagnostics" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
PerformanceCounter pc = new PerformanceCounter("System", "System Up Time");
pc.NextValue(); // discard the first value
TimeSpan ts = TimeSpan.FromSeconds(pc.NextValue());
Response.Write($"This system {Environment.MachineName} has been up for {ts.Days} days {ts.Hours} hours {ts.Minutes} minutes and {ts.Seconds} seconds.");
}
</script>
This page returns plain text indicating how long the server has been running.
π§© Step 2: Create the Classic ASP Page
Here is how to fetch and display the uptime from your uptime.aspx
file using AJAX inside a classic ASP file.
<div id="result"></div>
<script>
GetUptime();
function GetUptime() {
var xmlHttp = GetXmlHttpObject();
if (xmlHttp == null) return;
xmlHttp.onreadystatechange = stateChangedForUptime;
xmlHttp.open("GET", "uptime.aspx", true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");
xmlHttp.send(null);
}
function stateChangedForUptime() {
if (xmlHttp.readyState === 4 || xmlHttp.readyState === "complete") {
document.getElementById("result").innerHTML = xmlHttp.responseText;
}
}
function GetXmlHttpObject() {
var objXMLHttp = null;
if (window.XMLHttpRequest) {
objXMLHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}
</script>
When the ASP page loads, it automatically calls GetUptime()
which sends an AJAX request to uptime.aspx
. The response is then displayed in the result
container. This is a lightweight and interactive way to show real-time server uptime.
π§ Summary
The old method relies on a startup log file to calculate uptime.
The improved method uses
System.Diagnostics.PerformanceCounter
in an ASPX file.You can use AJAX in classic ASP to fetch and display the uptime.
This method works well even if your site is a mix of ASP and ASP.NET.
β Tips
Ensure
uptime.aspx
is deployed to a location accessible by your ASP pages.AJAX is a better choice than iframes due to its flexibility and control.
This technique avoids the need for file I/O or additional programs at system startup.
