Update Sep 18, 2016: This was my very old article. It might not work anymore. It is obsolete with today’s technology. Please use other technology rather than this. Thanks.
Mono is an open source project led by Novell (formerly by Ximian) to create an Ecma standard compliant, .NET-compatible set of tools, including among others a C# compiler and a Common Language Runtime. Mono can be run on Linux, BSD, UNIX, Mac OS X, Solaris and Windows operating systems.
With Apache mod_mono we can run ASP.NET with C# on a Linux machine. Mono is not just an ASP.NET interpreter; it also builds Windows Forms and runs on Linux, BSD, UNIX, Mac OS X, Solaris and Windows operating systems. But for now I just want to show you how to run an ASP.NET application on a Linux machine.
Ok, let’s start. Follow these steps (for Ubuntu):
1. You need to install several libraries for Mono. Type this command:
sudo apt-get install mono-xsp2 mono-apache-server2 libapache2-mod-mono mono-gmcs mono-utils
2. After it finishes you need to edit your mod_mono.conf. The easiest way to run an ASP.NET application is using AutoHosting configuration as shown below:
AddType application/x-asp-net .aspx .ashx .asmx .ascx .asax .config .ascx
DirectoryIndex index.aspx
MonoAutoApplication enabled
MonoServerPath "/usr/bin/mod-mono-server2"
You can read more about AutoHosting here or manual hosting configuration here; for Ubuntu users please refer to this documentation.
3. Save the file and restart your Apache.
4. Now let’s write the first ASP.NET application. You can copy the text below:
Filename: hello.aspx
<%@ Page language="c#" src="hello.aspx.cs" Inherits="HelloApp.HelloPage" AutoEventWireup="true" %>
<html>
<head>
<title>First Mono ASP.NET Application</title>
</head>
<body>
<form runat="server">
Enter your name: <asp:TextBox id="name" runat="server" />
<asp:Button id="greet" Text="Greet" onClick="OnGreetClick" runat="server"/>
</form>
<strong><asp:Label id="message" runat="server">Hello, World!
</asp:Label></strong>
</body>
<html>
Filename: hello.aspx.cs
using System;
using System.Web.UI.WebControls;
namespace HelloApp
{
public class HelloPage : System.Web.UI.Page
{
protected Label message;
protected Button greet;
protected TextBox name;
public void OnGreetClick(Object sender, EventArgs e)
{
message.Text = "Hello, " + name.Text;
}
}
}
5. Ok now save the file to your web server. Mine is at /var/www/firstaspnet/.
6. And you will see your first ASP.NET application running on Linux OS.
If you like this post, please leave me a comment. Have a nice day.
