C# Http server with ASP.NET
( August 18th, 2012 Networking ) - lo-fi/printable version

This tutorial will show you how to create a HTTP server that can parse ASP.NET files. It will be able to serve pages containing html,css and server-side code. As always I’ll try to keep the code simple – luckily .NET does all the work for us.

 

1. Preparing the project

Start by creating a Console Project. Once the project is created, go to Project->Project properties->Application and make sure that Target Framework isn’t set to a client profile framework:

Example: if it is .NET Framework 4 Client Profile change it to .NET Framework 4.

Now go to the Solution Explorer, right click on References->Add Reference->.NET->System.Web

Also, include the following namespaces:

 

2. Creating a simple server

Define a HttpListener object (that’s the server actually) and set it’s listening address to localhost.

Using an endless loop, this server will check for any incoming connections, if any connection is made, it will serve the page to the client, using a StreamWriter.

 

3. Embedding the ASP.NET Runtime

The lines above, which I said I’ll explain later are used for parsing the ASP.NET file: we can’t just send the file to the client, because it might contain server-side code, that can’t be interpreted by the browser.

Parsing the file is done using the following snippet:

This class called host, embeds the ASP.NET Runtime service. However this requires a custom AppDomain – otherwise it won’t work – so that’s the role of the line below:

3 arguments are required here, first is the type, the second is the virtual path and the third the physical path.

Ok, this is what you need to know before creating your ASP.NET server.

 

4. The complete code + bug fix
What you might not know is that there is a bug in .NET’s SimpleWorkerRequest – because of this bug, you can’t access pages that are in directories. If you have your asp file in a directory, you’ll get an 404 error – more information about this can be found here

This is the complete code of the server, that also fixes the problem:

 

5. Fixing the NotFoundException (error)

Yes, even if the code is correct, this error might appear:
“Could not load file or assembly ‘projectnamehere, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. The system cannot find the file specified.”

This is caused by ASP.NET Runtime – but it can be easily solved by creating a directory named bin and copying the executable there.

If you have your executable in the Debug folder:
“project_name/bin/Debug/asp_server.exe” <- original path

You have to create the Bin folder here (you’ll also have to copy the application in the new directory):
“project_name/bin/Debug/bin/asp_server.exe” <- new path
 

Now you can safely run your ASP.NET server, from the default location (not from the bin folder).

Note: if you change anything in the server's source and recompile it, you have to do the copy-paste thing again.

 

 

 

Later Edit: there seems to be a problem with the extensions – the server is only serving .aspx files (apparently files with other extensions can not be “seen”). Don’t know sure what can cause this…


2 comments
  • DamianH says:

    Nice. One question though… Is there a reason you are repeatedly creating the asphost each time in the while{} loop? Spinning up an appdomain is somewhat expensive – perhaps create the asphost once before entering the while{} loop?

    • Apex says:

      You’re right, it should be outside the while loop (for efficiency).
      I’ll update the code – thanks for pointing this out.

Have something to say?