Skip to content

Basic

Few lines of code to setup a REST API :

  • SimpleWServer : main class to manage the server (Start, Stop, Add Components)
  • Controller : base class for your middleware to inherit Request/Response properties
  • Route : attribut to target method of your middleware
  • Data : data return by middleware is default serialized to json and sent as reponse

The following code is full of comments :

csharp
using System.Net;
using SimpleW;

namespace Sample {
    class Program {

        static void Main() {

            // listen to all IPs on port 2015
            var server = new SimpleWServer(IPAddress.Any, 2015);

            // find all Controllers classes and serve on the "/api" endpoint
            server.AddDynamicContent("/api");

            // start non blocking background server
            server.Start();

            Console.WriteLine("server started at http://localhost:2015/");

            // block console for debug
            Console.ReadKey();

        }
    }

    // inherit from Controller
    public class SomeController : Controller {

        // use the Route attribute to target a public method
        [Route("GET", "/test")]
        public object SomePublicMethod() {

            // the Request property contains all data (Url, Headers...) from the client Request
            var url = Request.Url;

            // the return will be serialized to json and sent as response to client
            return new {
                message = Message()
            };
        }

        private string Message() {
            return "Hello World !";
        }

    }

}

Then just open your browser to http://localhost:2015/api/test and you will see the { "message": "Hello World !" } json response.

NOTE

The controller CAN NOT have constructor. All properties Request, Response (...) will be injected after session instanciation. See callback on how to inject specific code in all controllers of methods.

Released under the MIT License.