Getting ASP.Net (C#) running on Linux, using Mono and xsp2

I'm going to keep this brief, but hopefully explain how to get ASP.Net to work on a Linux machine. Your best choice here is to use Mono, which implements most of the important bits of ASP.Net (in fact, probably all of them, but don't quote me). So for starters, here's how to get a simple ASP.Net page up and running under Linux. (Note that I didn't enjoy the MonoDevelop experience much, and didn't find a decent C# plugin for Eclipse, so I'll be writing code direct for now.)

First off, you're going to need Mono itself. The download site is at http://www.go-mono.com/mono-downloads/download.html. However, there aren't any official package for Ubuntu there, so I tried http://www.mono-project.com/Other_Downloads instead. If you can find a package for your distribution, that may well work.

I struggled to compile the source for Ubuntu and couldn't get the debs to work (I've got an old version of Ubuntu), before I noticed there's a cross-platform Linux installer which worked fine for me. However, Michael Hutchinson mentioned in a comment below that this isn't such a good idea; his blog entry links to another set of installation instructions I didn't manage to find on the Mono site, which look promising. I'll try those next time.

Here's the link in case you want to try it:

http://ftp.novell.com/pub/mono/archive/1.9.1/linux-installer/2/mono-1.9.1_2-installer.bin

From a terminal, make it executable and run it:

chmod +x mono-1.9.1_2-installer.bin
./mono-1.9.1_2-installer.bin

This starts up the graphical installer. Follow the prompts to get it on your system. I recommend putting it into a directory out of the way of the standard Linux directory structure: I installed it into a directory within my home directory.

Next, create a directory for your project and put a basic ASP.Net page into that directory, so you can test that the Mono compiler's working properly. Create a file called index.aspx with this content:

<%@ Page Language="C#" %>
<html>
<head>
<script runat="server">
private void Submit(Object sender, EventArgs e) {
  button1.Text = "You clicked me!";
}
</script>
</head>
<body>

<h1>ASP.Net on Mono</h1>
<form runat="server">
<asp:Button id="button1" Text="Click me!" runat="server" OnClick="Submit"/>
</form>

</body>
</html>

I took this from the W3Schools tutorial on ASP.Net and modified it for C#.

To run your page, get a command line up and connect to your project's directory. Then run the embedded server xsp2 (bundled in the Mono installer), to start your application in its own web server:

xsp2 --port 9192

To see your site running, visit http://localhost:9192/ in a web browser. You should see a clickable button whose text changes when you click it.

That's enough for one night. Next time, where to put your code-behind pages, and perhaps how to get MySQL working with Mono.

Comments

I'd be interested to know...

As the author of ASP.NET support in MonoDevelop, I'd be interested to know what problems you had so that I can fix and improve things. If you could contact me/us by email, the mailing list or IRC that would be great :-)

I've worked on MD's ASP.NET support quite a lot since the 1.0 release -- the upcoming 2.0 release will even have code completion for ASP.NET, and outlining, folding, etc. -- but the fundamentals have not changed much since 1.0. The MonoDevelop ASP.NET tutorial explains a few things.

BTW, the cross-distro binary installer has been discontinued due to various subtle problems (mainly caused by people installing it to /usr and partially overwriting distro packages), so I'm glad it seems to have worked out for you.

Sorry, I also should have

Sorry, I also should have said what I didn't like about MonoDevelop. I think it was that it was giving me little over what I could get from a decent text editor, as I didn't seem to be getting code completion. Also, I couldn't find a specific ASP.Net project type (I think my experience was with an old version of MonoDevelop, as at the time I was using the distro package, and I'm on an old Ubuntu: hence I upgraded using the binary installer). I think I should give a more recent version a go before I comment in more depth, as I'm sure things have moved on. Also, I know Eclipse pretty well, so my personal preference would be to use a plugin, if such a thing exists.

Michael, thanks for the

Michael, thanks for the great comment. Extremely helpful. I think you're right: I get a few odd error messages occasionally from apps. about Mono libraries being in the wrong place (I removed Mono altogether and installed into a local directory instead), so I guess the binary installer has wrecked something. And when I try to run monodevelop from the directory I installed into, I get an error where it tries to connect to /usr/lib/monodevelop/bin (which doesn't exist). So I'll probably try the instructions you mentioned instead, to get a clean install.

Sounds like the version of 2.0 will be far more useful, and I'll definitely keep an eye on your blog for release announcements etc.. Good work on it so far: I'm certainly enjoying learning it.