C# Http Server With ASP.NET

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

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

hostasphost=(host)ApplicationHost.CreateApplicationHost(typeof(host),"/",Directory.GetCurrentDirectory());//the code above will be explained later
while(true){HttpListenerContextcontext=server.GetContext();//the context
HttpListenerResponseresponse=context.Response;//this will specify where to send the data
StreamWritersw=newStreamWriter(response.OutputStream);//data is sent using a streamwriter
stringpage=context.Request.Url.LocalPath.Replace("/","");//this gets the file requested by the client
stringquery=context.Request.Url.Query.Replace("?","");//and this will store any GET parameters -- not very important
asphost.parse_code(page,query,refsw);//parses the page and sends it
sw.Flush();context.Response.Close();//closes the connection, once the page was sent
}

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.

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

usingSystem;usingSystem.Net;usingSystem.IO;usingSystem.Web;usingSystem.Web.Hosting;namespacetest{classProgram{staticvoidMain(string[]args){HttpListenerserver=newHttpListener();server.Prefixes.Add("http://127.0.0.1/");server.Prefixes.Add("http://localhost/");server.Start();hostasphost=(host)ApplicationHost.CreateApplicationHost(typeof(host),"/",Directory.GetCurrentDirectory());while(true){HttpListenerContextcontext=server.GetContext();HttpListenerResponseresponse=context.Response;StreamWritersw=newStreamWriter(response.OutputStream);stringpage=context.Request.Url.LocalPath;stringquery=context.Request.Url.Query.Replace("?","");asphost.parse_code(page,query,refsw);sw.Flush();context.Response.Close();}}}classWorkerRequest:SimpleWorkerRequest//the fix, use this instead of SimpleWorkerRequest
{stringpage=string.Empty;publicWorkerRequest(stringpage,stringquery,TextWriteroutput):base(page,query,output){this.page=page;}publicoverridestringGetFilePath(){returnbase.GetFilePath()+page;}}classhost:MarshalByRefObject{publicvoidparse_code(stringpage,stringquery,refStreamWritersw){WorkerRequestswr=newWorkerRequest(page,query,sw);//replacing SimpleWorkerRequest
HttpRuntime.ProcessRequest(swr);}}}

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…