Create your own Web Server using C#

Configuration

First we will define the root folder for our web server. Eg: C:\MyPersonalwebServer, and will create a Data directory underneath, our root directory Eg: C:\MyPersonalwebServer\Data. We will Create three files under data directory i.e.

Mimes.Dat
Vdirs.Dat
Default.Dat

Mime.Dat will have the mime type supported by our web server, Syntax:  <EXTENSION>; <MIME Type>e.g.
.html; text/html
.htm; text/html
.gif; image/gif
.bmp; image/bmp

VDirs.Dat will have the virtual directory Information. Syntax: <VirtualDir>; <PhysicalDir>  e.g.
/; C:\myWebServerRoot/
/test/; C:\myWebServerRoot\Imtiaz\

Note: We have to include all the directories used by our web server, for example, if the html page contains reference to images and we want to display image, we need to include it also. e.g.
/images/; c:myWebServerRoot\Images\, else that directory won't be accessed..

Default.Dat will have the virtual directory Information; e.g.
default.html
default.htm
Index.html
Index.htm;

We will store all the information in plain text file for simplicity, we can use XML, registry or even hard code it. Before proceeding to our code let us first look the header information which browser will pass while requesting for our web site

Let say we request for test.html we type http://localhost:5050/test.html
(Remember to include port in the url), here what the web server gets..

GET /test.html HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Accept-Language: en-usAccept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0; .NET CLR 1.0.2914)
Host: localhost:5050
Connection: Keep-Alive

You might also like...

Comments

About the author

Imtiaz Alam United States

Imtiaz Alam is a Senior Developer, currently residing in Phoenix, Arizona. He has more than five years of development experience in developing Mirosoft based Solution.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only two kinds of languages: the ones people complain about and the ones nobody uses” - Bjarne Stroustrup