Introduction to Mono - ASP.NET with XSP and Apache

The second article in a series of articles about Mono. This article explains how to host and serve ASP.NET Web Applications and Web Services on Linux using XSP and Apache with the help of Mono.

Introduction

This article is the second article in the series of introductory articles that I am writing about Mono, the "open source development platform based on the .NET framework". In this article we'll take a look at how to get going with ASP.NET on the Mono platform. Although ASP.NET is not part of the ECMA and ISO standards mentioned in the first article[^], it is still one of the major selling points of the .NET platform and provides an extremely flexible and powerful platform for developing web applications and Web Services upon. Although you can develop ASP.NET applications for Mono on a number of different operating systems this article will focus mainly on Linux, although, in saying that, I do look briefly at getting XSP running on Windows. The reason I will concentrate on ASP.NET on Linux is because those people interested in ASP.NET on Windows have an extremely powerful option at their fingertips in the form IIS and I would whole heartedly recommend using it for ASP.NET on Windows.

Where does ASP.NET stand with Mono??

The latest stable version of Mono, version 1.0.5, has a fully functional implementation of ASP.NET. This includes full support for ASP.NET Web Forms and Web Services. This essentially means that more or less any ASP.NET application that you have developed using with the .NET Framework will work with Mono. Obviously there might be changes needed, such as data access changes, removal of any reliance on .NET Framework BCL types that are not implemented in Mono yet, and also the removal of any code that makes use of platform invoke and so on. At this stage the ASP.NET support in Mono can be considered as excellent and a lot of publicly available web applications already make use of Mono's ASP.NET support. According to the Mono site the Mono Web Services stack is being used in the source control application Vault[^] by SourceGear and aspects of Mono's ASP.NET implementation are also used in the Virtuoso[^] product from OpenLink.

There isn't much point in developing web applications and Web Services if you have no way of serving them, is there? Traditionally you would use IIS to host ASP.NET applications on Windows, although there are some other free ASP.NET web servers available such as Cassini[^]. However, when using ASP.NET with Mono you have two main options as regards which web server to host your ASP.NET applications in:

XSP

Apache

In this article we look at using both XSP (on Windows and Linux) and Apache to host your ASP.NET web applications and Web Services.

XSP

XSP is a "light-weight web server" capable of hosting and serving ASP.NET applications. It is written using C#, making extensive use of the classes in the System.Web namespace, and the source for XSP is available for download from the Mono download page[^] . It is also interesting to note that the XSP web server runs on both the Mono runtime and also on the .NET runtime which means that if you are looking for a light-weight ASP.NET web server for Windows but you don't want to use or you don't care about Mono, you could still make use of XSP. The Let's get XSP up and running section runs through how to install and use XSP on Windows and Linux.

Apache

Apache is probably the de facto standard web server used on Linux. Apache makes extensive use of modules to enable it to host and serve web applications developed in a multitude of different web programming languages and scripts. Those of you familiar with IIS but not so familiar with Apache can think of these modules as the equivalent of ISAPI extensions. So, by this stage I assume you can guess how Apache can host and serve ASP.NET application? Yep, using an Apache module. This module, called mod_mono, allows Apache to serve ASP.NET pages "by proxying the requests to a slightly modified version of XSP called mod-mono-server". mod-mono-server is installed when you install XSP. At the time of writing the current version of the mod_mono module for Apache only works for Apache on Linux and not for Apache on Windows. For this reason, when we look at ASP.NET on Apache in the Apache with mod_mono section we only look at it in the context of Linux. If you do wish to serve ASP.NET content from Apache on Windows have a look at this article[^] on CP, which shows how to use configure Apache to use Cassini to serve ASP.NET content or have a look at the Apache HTTP CLI[^] project.

This article assumes that you have Mono installed and working on your desired platform. The first article in this series, Introduction to Mono - Your first Mono app[^], explains how to get Mono up and running on Windows and Linux so if you have not yet done so have a look over that article. I also assume that you are familiar with ASP.NET and the semantics of ASP.NET programming as this article primarily looks at getting ASP.NET working with XSP and Apache, not at ASP.NET programming.

In this section we will look at getting XSP up and running on both Windows and on Linux. Installing and running on Windows is "tackled" first simply because I want to get it out of the way and because chances are that you already have XSP on Windows considering the fact that you should have Mono already installed. Then it's on to Linux and getting XSP up and running there. To be honest, if you already have Mono running on Linux, which you should as it's one of the prerequisites for this article, then it should be relatively easy.

XSP on Windows

The first article[^] in this series ran through installing Mono on Windows. It mentioned that during the install process the installer gives you the option to select which components to install. By default, XSP is installed along with Mono but if you decided to not install XSP during the install then the easiest way to get XSP installed is to uninstall Mono and then reinstall it again. Here are the steps explaining how to install Mono and XSP on a Windows 2000 or above machine:

The installer is a standard Windows installer so you can simply run it once it is downloaded by doubling clicking on it.

Once the installer begins, click on next to go to the License Agreement page.

You'll probably notice on the License Agreement page that there are a number of different Licenses that apply to the different components of the install. While I understand that most people simply agree and click on next, it would be irresponsible of me if I didn't recommend that you read the License Agreement before agreeing to it.

Once you pass the License Agreement section you are presented with some general information about the install.

Click on Next and select the install location.

Click on Next again to bring you to the Select Components page. You are free to customize your installation but make sure that you install XSP. As I will be using all the components of this install in other articles in this series I would suggest that you accept the default "Full installation".

Click on Next again and you can customize the name of Mono folder that appears in the Start Menu.

The installer will now ask you which port you want the XSP web server to listen on. Generally this will be port 8088 but you can change it to any free port on your system. When you are ready click on Next.

Finally, click on Install to install and configure Mono and XSP on your Windows machine.

Those of you who read the first article will notice that I more or less ripped the instructions from that one... I'm not lazy, just no point in rewriting what has already been said! The next step in using XSP on Windows is to run it. The Running XSP section explains how to run the XSP web server and looks at some of the command line options that you can use to alter it's default behaviour.

As with installing Mono on Linux, which we examined in the previous article, things are slightly more complicated on Linux then on Windows. However, as I already mentioned, if you have Mono installed, then getting XSP installed is going to be a breeze for you. I'm installing XSP on the same systems that I used for installing Mono in the previous article. A quick recap of what exactly I used is in order so: I used Microsoft Virtual PC 2004 and created two identical virtual PCs. I then installed SuSE 9.2 Professional with ACPI disabled and I used the default package selection when installing. In the last article I went through how to install Mono using the pre-built packages from the Mono site and also I ran through how to install Mono from source. So for the purposes of this article I will be installing XSP on a SuSE 9.2 Professional with the default package selection with Mono 1.0.5 installed along with any additional packages or programs that were installed during the Mono install.

In this article we will again look at installing from both the pre-build packages and from the source.

XSP using pre-built packages

As I am sure you can remember from the first article (if you read it that is), installing using the pre-built packages from the Mono site is one of the easiest ways of getting Mono up and running. The same thing applies to getting XSP up and running. So, head on over to the Mono download page (http://www.mono-project.com/downloads/index.html[^]) and see if there are pre-built packages for your particular Linux distribution or if there are packages for a distribution that you know will be compatible with your distribution. If there isn't a package that will work with your distribution just jump ahead to the XSP from source section where I will run through installing XSP from the source which is a simple process.

Lets get going:

On the Mono download page, click on the "Packages" link beside the Linux distribution that you believe to be compatible with your distribution. I simply selected the link beside SuSE 9.2

This should bring you to a page which lists a lot of different packages that have been compiled and packaged for that particular Linux distribution.

Before we install XSP we need to install two other pre-built packages. These packages include files that XSP is dependent upon.

As with the two previous packages, simply open it and follow the instructions of your package manager to install XSP.

All going well, you should have XSP installed now!

At this stage XSP should be installed on your system so you can jump ahead to the Running XSP section which gives an overview of how to use XSP. If you are having difficulty installing XSP from the packages have a look at the next section, "XSP from source", which might be able to help you out.

Once the file has been downloaded, go to a console for the remainder of the process

You can decompress the downloaded file using the command tar -xvzf xsp-1.0.5.tar.gz where xsp-1.0.5.tar.gz is the name of the file you downloaded.

Next, switch to the directory where the source code has been decompressed to by typing cd xsp-1.0.5 or cd followed by the name of the directory that you decompressed the file to if it is different from mine.

Now, before we can compile the source you need to configure the make files. To do this simply type ./configure --prefix=/usr. This will take a bit of time. I am assuming here that there were no errors and that the ./configure --prefix=/usr went ahead without issue.

When the the ./configure --prefix=/usr completes you are free to go ahead and compile and install the source.

To actually compile the XSP source you need to run the make command.

Root privileges are required for the next step. If you have just installed Linux as a standalone machine you can usually give your account root privileges by typing the sudo bash command and entering the root password.

Finally, to install, for lack of a better word, simply run the make install command. This will deploy the compiled binaries, libraries and other bits and pieces to the correct directories.

Congratulations. You should not have the XSP web server installed on your system. The next section looks at how to run and use the XSP web server.

XSP should be installed on your preferred platform at this stage so now we will look at how to start XSP and we will also look at the different command line options you can use to modify the default behaviour of the server.

Running XSP ASP.NET Examples

Starting XSP with the ASP.NET examples that come with it is a bit of a no-brainer on both Linux and Windows, well, it is with the version 1.0.5 of XSP that I am using:

Starting XSP examples site on Windows

Simply go to to the Mono folder in the Programs or All Programs section of your Start Menu. For example, on my machine the Mono folder in the All Programs folder on the Start Menu is called Mono 1.0.5 for Windows.In this folder, go to the XSP subfolder and then click on XSP Test Web Server.

Starting XSP examples site on Linux

Starting the XSP server with the default settings on Linux is easier again. Simply change directory to the directory where the ASP.NET examples are installed using the command cd /usr/share/doc/xsp/test/ (this assumes that you have installed XSP in the /usr diretory).Now, to start XSP with the ASP.NET examples simply use the command xsp which will start XSP using the default settings.

On both Windows and Linux you should get output similar to the following on your screen:

The above output is from starting XSP on Linux using the steps above. The main difference you will notice in the output from Windows is that the port that the server is listening on will probably be port 8088, which is the default port used by the Mono installer on Windows. You will also notice that the Root Directory will probably be something like C:\PROGRA~1\MONO-1~1.5\share\doc\xsp\test.

Regardless of the output, if XSP has started successfully simply fire up your browser and point it to http://localhost:<port num> where <port num> is the port number that is listed at the end of the the Listening on port: line of the output. On Linux you would generally use http://localhost:8080 and on Windows you would use http://localhost:8088. When the browser loads you should be presented with a web page similar to the one shown in Figure 1 below.

Figure 1: XSP web server on Linux showing ASP.NET examples pages.

The page shown in Figure 1 is the home page of the example ASP.NET site that comes with XSP. It has numerous examples of ASP.NET Web Forms and ASP.NET Web Services.

XSP, close up.

The information in the section above shows how to start the examples website that comes with the XSP web server. However, chances are high that you will want to use the web server for actually serving your own web applications and Web Services. To this end we will now look at the command line options that you can use to modify the default behaviour of XSP. However, before doing that I want to point out where the XSP web server itself is located on both Linux and Windows:

I would recommend adding the directory <Mono install dir>\lib\mono\1.0\ to your path in Windows as this will allow you to start the XSP web server by typing xsp.exe at the command prompt. You should note however that if you do start XSP in this manner you will actually be running it on the .NET runtime and not on the Mono runtime... a good example of Mono - .NET interoperability.

Linux

<Mono install dir>/lib/mono/1.0/xsp.exeOn my installation, using the instructions in the XSP on Linux section above, this related to: /usr/lib/mono/1.0/xsp.exe

Note that on Linux the installation also places a script <Mono install dir>/bin/xsp that can be used from the console to start XSP. This is the command I used in the section above to start the XSP examples web site. When you type and run xsp at the console you are actually running the command mono /usr/lib/mono/1.0/xsp.exe

Now, on to the command line options for XSP, or to be more exact, for xsp.exe:

Now that we have looked at XSP it's time to move on to getting Apache serving ASP.NET pages. If you don't care about ASP.NET on Apache then you could always skip ahead to the Some Examples section where I run through a simple ASP.NET example.

Apache is the web server of choice on Linux and is, according to netcraft[^], the most widely used web server on the net. This means that there are lots of websites out there that could be serving ASP.NET web applications and hosting ASP.NET Web Services. Mono makes this possible in the form of the mod_mono module for Apache which makes use of XSP or, to be more specific, it makes use of mod-mono-server.exe which is a specialized version of XSP.

In this section we will look at installing mod_mono and how to get Apache serving ASP.NET content. However, before we do that, we need to get Apache installed. As this article is aimed more at how to get Apache serving and hosting ASP.NET content I will quickly run over getting Apache installed. If you run into any difficulty when trying to install Apache just search the web for instructions on how to install Apache on your particular Linux distribution.

Oh, and one more thing. As I mentioned in the What are XSP and mod_mono? section, mod_mono currently does not work with Apache on Windows so I only look at Apache on Linux from here on.

As with most things on Linux there are a number of different ways to install applications. In the case of Apache I'm just going to install from source as it is probably one of the easiest ways to install Apache and it is also the most distribution independent way of installing it that I know of. If you already have Apache 1.3.x or greater installed then you can skip ahead to the Installing and configuring mod_mono section. So, lets get Apache installed:

Once the file has been downloaded, go to a console for the remainder of the process

Decompress the downloaded file. For example, if you have downloaded the same file that I downloaded you could use the command tar -xvzf httpd-2.0.53.tar.gz as long as the file is in your current working directory.

Next, switch to the directory where the source code has been decompressed to by typing cd httpd-2.0.53 or cd followed by the name of the directory that you decompressed the file to if it is different from mine.

Now, before we can compile the source you need to configure the make files. To do this simply type ./configure --enable-so --prefix=/usr.

When the the <CODE>./configure --enable-so --prefix=/usr completes you are free to go ahead and compile and install the source.

Run the make command to compile Apache.

You need to have root privileges to perform the next step successfully. If you have just installed Linux as a standalone machine you can usually give your account root privileges by typing the sudo bash command and entering the root password.

Finally, to install, for lack of a better word, simply run the make install command. This will deploy the compiled binaries, libraries and other bits and pieces to the correct directories.

At this stage you should have Apache installed. To start the Apache web server type httpd at the console. You can check if Apache has started by pointing your browser to http://localhost/ where you should see a web page similar to the one shown in Figure 2.

Figure 2: Apache Web Server running on Linux

Now that we have Apache installed it's time to install mod_mono and configure Apache to host and serve ASP.NET content.

Ok. We're nearly there. Apache should be up and running by now so we don't have too much left to do. We need to install mod_mono and then configure Apache in order to make it send the requests that we want through the mod_mono module and on to the mod-mono-server.exe application that will handle the request. As with Apache, I'm gong to run through installing mod_mono from the source. Before we actually go ahead and install mod_mono I should mention that there are some prerequisites to installing. You must have Mono installed, you must have XSP installed, and you must have Apache installed. If you've been following this article, and the previous article, then you should already have your system configured as needed.

Once the file has been downloaded, go to a console for the remainder of the process

Decompress the downloaded file using the command tar -xvzf mod_mono-1.0.5.tar.gz where mod_mono-1.0.5.tar.gz is the name of the file you downloaded.

Next, switch to the directory where the source code has been decompressed to by typing cd mod_mono-1.0.5 or cd followed by the name of the directory that you decompressed the file to if it is different from mine.

Now, before we can compile the source you need to configure the make files. To do this simply type ./configure --prefix=/usr .

Next, to actually compile the mod_mono source you need to run the make command.

The next step has to be performed using an account with root privileges. If you have just installed Linux as a standalone machine you can usually give your account root privileges by typing the sudo bash command and entering the root password.

Finally, to install mod_mono simply run the make install command. This will deploy the compiled binaries, libraries and other bits and pieces to the correct directories.

That should have been painless. You should have mod_mono installed on your system now. So what's left to do? Well, we need to configure Apache to use mod_mono to handle requests. This means we need to edit the Apache config files. If you have installed Apache following the instructions in the Installing Apache section the Apache config file should be the following file: /usr/conf/httpd.confObviously the location of this file may be different if you installed Apache in a different location or if you installed it using a different method.

Firstly, we need to configure Apache to load the mod_mono module. To do this we need to edit the Apache config file, /usr/conf/httpd.conf, and add the following line to the end:

LoadModule mono_module /usr/modules/mod_mono.so

The above line tells Apache to load the module located at /usr/modules/mod_mono.so as the mono module.

The next thing that we need to do it configure Apache to serve some ASP.NET content. We will use the same ASP.NET Examples that we used with the XSP server above. To do this we need to tell Apache to serve content from the /usr/share/doc/xsp/test/ directory for a given path, lets say, /AspNetOnApache. This means that when you browse to http://localhost/AspNetOnApache you will be served the ASP.NET content in the /usr/share/doc/xsp/test/ directory.

and for Apache 2.0.x you can use the above text OR you can use the following

<Location /AspNetOnApache>
SetHandler mono
</Location>

Finally! We are there. Simply go to the console and type the command httpd to start Apache. Point your browser to http://localhost/AspNetOnApache and you should see a page similar to the one shown in Figure 3.

So after all that you should be able to serve ASP.NET content from at least one server on Linux. Just to help round off the "experience" I've decided to add a basic web application example to this article.

A simple web application example

Our simple web application is very simple indeed. It consists of a single page that displays some basic information about the machine that the page is hosted on and also some basic information about the request for the page. There are two files, index.aspx and index.aspx.cs, the former being the web page and the latter being the codebehind file for that page. So open up your favourite text editor or IDE and create the two files (hint: copy and paste is probably the quickest way):

Once you have saved these two files we now need to be able to serve it. I saved the files to /home/bdelahunty/mono/webappexample/ and we now need to configure both XSP and Apache to serve pages from this location. As this is a simple web application example lets call it SimpleWebApp.

To get XSP to serve the page use the following command at the console (replacing /home/bdelahunty/mono/webappexample/ with the directory where you saved the files):

xsp --applications /SimpleWebApp:/home/bdelahunty/mono/webappexample/

To get Apache to serve the page simply add the following to the end of the Apache config file, /usr/conf/httpd.conf in my case (replacing /home/bdelahunty/mono/webappexample/ with the directory where you saved the files):

Problems with the Visual Studio .NET and IIS way?

If you are used to developing ASP.NET web applications with Visual Studio .NET then there are a few things you need to watch out for. When creating aspx pages Visual Studio .NET used the CodeBehind attribute in the pages @Page directive. However, CodeBehind is not actually an official ASP.NET @Page directive attribute and is only used by the IDE at design time and when compiling the ASP.NET application to a dll. You would normally then drop this compiled dll into the bin directory of your web application and IIS would automatically pick up the changes. This doesn't work with XSP or mod_mono; you can't just drop in a new pre-compiled dll and have the web server start using it.

You may or may not have noticed that I used the Src attribute in the @Page directive in the simple example above and I did not give any details about pre-compiling the web application to a dll that has to be deployed. I just pointed XSP and Apache to the directory where the index.aspx and index.aspx.cs files where located and the web servers handled the compilation of the web application the first time the page was called. This is one way you can get around the pseudo problem of not being able to just drop in a new pre-compiled dll. You can just drop in a new .cs codebehind file and the web server will compile it on the fly. This method does however require that you have all your codebehind files on the server. If you setup your permissions correctly then this is not an issue, if you don't, it's a big issue as any visitor to your site could potentially view the code behind your site and potentially come up with ways to hack it.

If you are used to the Visual Studio .NET and IIS way, or if you simply use Visual Studio .NET to build your web applications and want to just drop the pre-compiled dll into the bin directory then you can quickly overcome the problem by deploying your new dll and then restarting the web server. Use httpd -k restart to restart Apache after you have put in a new dll and if you are using XSP just shut it down and then restart XSP it.

What next?

Well hopefully this article has helped you to get some ASP.NET pages served from a Linux machine running XSP or Apache (and of course from XSP on Windows) and that it has given you a basic overview of using XSP and mod_mono. The next article in this "Introduction to Mono" series will either look at how you can get the latest version of Mono at any time using CVS or how to develop some GUI applications using Mono.

Note

When this article was written the latest stable version of Mono was 1.0.5. However, at the time of posting this article the latest stable version is 1.0.6.

History

March 3rd, 2005 - First Posted.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

i am new to linux .i downloaded a linux suse VM machine with mono2.10.

here my asp.net application is developed under .net framework 4.0.its working fine under windows.i want to test that asp .net application under mono on linux .
will any body pls tell me the hosting process under linux suse.
how to host and where should i place my application dlls .what are the configurations i need to do ?pls help me out...

I am trying to run this sample code with the .NET version 2 equivalent of mono and XSP2. When I load up the page in the browser, it throws up an error -error CS0102: The type `SimpleWebApp.SimplePage' already contains a definition for `serverName'

I have a ASP.NET Web Service running on Apache (Mono) on a Linux machine. One of my Web Methods that performs writing to disk generates error:

The remote server returned an error: (500) Internal Server Error.
Access to the path "filename.ext" is denied.

I suppose this is due to the fact that the ASP.NET account doesn't have write access? I am not a Linux guy at all, so I have no idea of where and what to change in order to make it work. Also, my Linux administrator is completely new to .NET and knows nothing about Mono. Therefore, I will have to give him very specific instructions to work on. Could you please point me to the right direction? Any help would be highly appreciated.

Hi, I've been very interested in Mono lately. I've been evaluating Mono's ASP .NET implementation for a small business I might start. It's good to know that ASP 1.1 is completely supported by Mono(1.1.10), but I'm a bit concerned about XSP/mod_mono's reliability. The only thing that has ever been said about XSP is that it is lightweight, but I've yet to read about XSP's security, performance or overall reliability as a web server. Do you think XSP/mod_mono is a viable solution to host a "real life" site (i.e. The Code Project) with a large number of users? thanks

Is mono XSP web server does not support ASP.net code-behind? the directive work well in IIS web server! I used mono MCS command in MS command prompt to compile this C# code-behind application...
anyone can tell me how can i run asp.net code-behind application in mono XSP web server?