A Simple Web API Builder in C#

What I wanted was a way to quickly prototype my web projects that need some C# interaction. What I came up with was a small library that allows me to quickly deploy a self hosted C# based web API.

I started with a neat little embedded http server I found on codeproject. From there I built a small wrapper around the embedded server and compiled it into a standalone library.

How it works…

After adding a reference to the supplied “APIBuilder.dll”, add the namespace “APIBuilder” and you will now have access to two items.

  • A class named HttpEndpoint
  • An Interface named Location

The general idea is that you have an HTTP webserver or “endpoint” bound to a specific port and you have Location objects that service requests bound to certain locations in that endpoint.

For example, the following code initializes a new endpoint at port 5000 and binds a Location object to the location “/add”. When the location gets requested, the paramerters get passed into the Location objects request method and the returned string of that method is returned back to the client.

class Program
    static void Main(string[] args)
        //create a new HttpEndpoint and bind a new add instance to the location "/add"
        HttpEndpoint myEndpoint = new HttpEndpoint(5000);
        myEndpoint.bindLocation("/add", new add());
        Console.WriteLine("Service running... Press enter to quit.");
class add : Location
    public override string request(System.Collections.Hashtable data)
        //extract post params and convert them to double
        double value1 = Double.Parse(data["value1"].ToString());
        double value2 = Double.Parse(data["value2"].ToString());
        double answer = value1 + value2;
        return answer.ToString();

If we ran the above code, we would be able to post a form with 2 values (value1 and value2) to “localhost:5000/add” and be returned the two values added together.

The library supports both GET and POST with POST taking precedence. Also, any language that can send a request via http can communicate with the API; I built it originally with the intention of using php’s curl to communicate with it.

This method makes creating quick and dirty web APIs extremely easy and I hope you enjoy it as much as I do.


NOTE* – I have tested the library in mono and all seems to be functional in versions > 2.8.


I created a sample interface for this in PHP.