Getting started

The following sample will create and start a very simple web server which will host static content in the directory c:\minnowroot on the url http://localhost/Temporary_Listen_Addresses

WebServer myWebServer = new WebServer();
myWebServer.LocalRoot = @"c:\MinnowRoot";
myWebServer.Prefixes.Add("http://+:80/Temporary_Listen_Addresses/");
myWebServer.RootUrl = "/Temporary_Listen_Addresses/";
myWebServer.HandlerMappings.Add(new HandlerMapping("*", new StaticHandler()));
myWebServer.Start();
Console.ReadLine();

Whats going on here?

Breaking it down line by line:

WebServer myWebServer = new WebServer();
The WebServer class encapsulates the web server itself.

myWebServer.LocalRoot = @"c:\MinnowRoot";
The LocalRoot property is the path to the local directory from which the web server should serve up content. It isn't necessary to specify a local root, however many handlers will require one in order to function.

myWebServer.Prefixes.Add("http://+:80/Temporary_Listen_Addresses/");
The collection of Prefixes a the collection of addresses on which the web server should listen for requests - under the covers this is simply the Prefixes property of the underlying HttpListener class.

The prefix http://+:80/Temporary_Listen_Addresses/ was chosen as an example as on most machines you do not need elevated permissions in order to listen on this prefix.

For more information on Prefixes, refer to the MSD documentation:

myWebServer.RootUrl = "/Temporary_Listen_Addresses/";
The RootUrl is an indicator to the web server on how to map request URLs onto the local root directory. The above RootUrl indicates that the request URL http://servername/Temporary_Listen_Addresses/ should map onto the local directory c:\MinnowRoot.

If the RootUrl is not specified then the web server maps all request URLs onto local files by their absolute URL, so for example if RootUrl was null or empty in the above example then the request URL http://servername/Temporary_Listen_Addresses/myfile.html would map onto the local path c:\MinnowRoot\Temporary_Listen_Addresses/myfile.html

It isn't necessary to start and end the RootUrl with '/', the web server will add these automatically.

myWebServer.HandlerMappings.Add(new HandlerMapping("*", new StaticHandler()));
This adds a handler mapping to the server - the WebServer class itself doesn't serve up any web pages (other than error pages), it delegates the task of serving up content to handlers. In the above example we instruct the web server to use the StaticHandler (a handler which serves up static files unmodified to the end user) for all request URLs.

See the documentation page on Handlers for more detailed information on handlers.

myWebServer.Start();
Console.ReadLine();
Finally, we call the Start() method to start the web server - the Start method is non-blocking, so the sample application does Console.ReadLine() to prevent the application from exiting as soon as the web server starts up.

What now?

The above web server will only serve up static content - if you want to serve up more interesting content (such as Asp.Net or FastCGI applications) then you need to use the more complex handlers.

Alternatively you can start with the sample application (MinnowConsole) which should be able to handle some common web application types without too much configuration.

Last edited Aug 3, 2010 at 10:09 AM by Kragen2uk, version 5

Comments

No comments yet.