Creating a Simple Chat Application in .NET - Step-by-Step Guide
Last Updated: Nov 03, 2025
5 min read
Legacy Archive
Legacy Guidance:This article preserves historical web development content. For modern .NET 8+ best practices, visit our Tutorials section.
Chat applications are found in almost all sites that are interactive with users. Chat rooms in websites provide a means of communicating with the customer service cell of any company so that customers get firsthand information on products and services displayed on the site.
Chat applications with many features are sold for a price, but some sites may require very simple features to interact with customer service personnel who are online. Such simple chat applications can be built on your own. Let's look at the algorithm and code snippets that go into creating a simple chat application.
Choosing Your Message Storage Approach
To store the messages typed during a chat, you can either use static arrays or use a database for that purpose. Using a database is a good way of storing messages for production applications. However, you can also use static arrays for simple implementations, which is what we'll focus on here.
The chat application we're going to build will work on any browser that supports iFrame, since we're using iFrame to display the chat window. We'll refresh the page in the iFrame to refresh the chat window alone without refreshing the whole page where the iFrame is present.
Creating the Chat Entry Page
To enter the chat, you'll need an ASPX page where users enter the name of the room and their user ID. Then the user presses a Join Chat button. You can pass these parameters to the ChatPage.aspx where you'll have an iFrame for displaying messages, a textbox control to type messages, and a button to send the typed messages.
JoinChat.aspx.cs
private void Join_Click(object sender, System.EventArgs e)
{
// Redirect to chat page with room and user parameters
Response.Redirect("ChatPage.aspx?Room=" + Room.Text +
"&User=" + UserID.Text);
}
Building the Chat Management Class
For creating the chat application, you'll need a class that performs various functions like adding messages to a static array. You can create any class for this purpose. You'll need methods for adding messages, retrieving all messages typed, and displaying messages in the chat window.
ChatManager.cs
using System.Collections;
public class ChatManager
{
static protected ArrayList stArray = new ArrayList();
static public void AddMessage(string sRoom, string sUser, string sMsg)
{
string sAddTxt = sRoom + ":" + sUser + ":" + sMsg;
stArray.Add(sAddTxt);
// Keep array size manageable
if (stArray.Count > 100)
{
stArray.RemoveRange(0, 20);
}
}
static public ArrayList GetMessages(string sRoom)
{
ArrayList roomMessages = new ArrayList();
foreach (string msg in stArray)
{
if (msg.StartsWith(sRoom + ":"))
{
roomMessages.Add(msg);
}
}
return roomMessages;
}
}
Implementing the Send Message Functionality
Within the ChatPage.aspx, once you type the message to be sent and click the Send button, you'll need to add the message to the static array. You check the textbox for content, and if it contains any text, you add the message to the array.
ChatPage.aspx.cs
private void Send_Click(object sender, System.EventArgs e)
{
string sRoom = "";
string sUser = "";
// Get room from query string
if (Request.Params["Room"] != null)
sRoom = Request.Params["Room"].ToString();
else
sRoom = "1";
// Get user from query string
if (Request.Params["User"] != null)
sUser = Request.Params["User"].ToString();
else
sUser = "Anonymous User";
// Add message if textbox has content
if (TB_ToSend.Text.Length > 0)
{
ChatManager.AddMessage(sRoom, sUser, TB_ToSend.Text);
TB_ToSend.Text = "";
}
}
Auto-Refreshing the Chat Display
You can use the following code to refresh the page in the iFrame to display all the messages that have been typed. This creates a continuous update cycle that shows new messages as they arrive.
DisplayMessages.aspx
// Auto-refresh every 4 seconds
Response.Write("<meta http-equiv=\"Refresh\" content=\"4\">");
// Display all messages for this room
string sRoom = Request.Params["Room"];
ArrayList messages = ChatManager.GetMessages(sRoom);
foreach (string msg in messages)
{
// Parse and display each message
string[] parts = msg.Split(':');
if (parts.Length >= 3)
{
Response.Write("<b>" + parts[1] + ":</b> " +
parts[2] + "<br/>");
}
}
Setting Up the Chat Page HTML
Your ChatPage.aspx should contain an iFrame that points to the message display page, along with controls for entering and sending messages. The iFrame will automatically refresh to show new messages.
You can create a simple chat application that can be used in all sites that need interaction with users. While this implementation is basic, it provides the foundation for understanding how chat applications work. For production environments, you'd want to add features like database persistence, user authentication, and real-time updates using modern technologies like SignalR or WebSockets.
FAQ
Should I use a database or static arrays to store chat messages?
For simple chat applications, static arrays work fine and are easier to implement. However, using a database is better for production applications as it provides persistence, allows message history, and scales better with multiple users.
Why use iFrames for the chat window?
iFrames allow you to refresh just the chat window without refreshing the entire page. This keeps the user's input field intact while displaying new messages, creating a smoother chat experience on browsers that support iFrame functionality.
How do I prevent the message array from growing too large?
Implement a check that monitors the array count. When it exceeds a limit (like 100 messages), remove the oldest messages using RemoveRange. This keeps memory usage under control while maintaining recent chat history.
Can this chat application work across different browsers?
Yes, this chat application works on any browser that supports iFrame elements. Most modern browsers support iFrames, but you should test your application across different browsers to ensure compatibility.