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 propertiesRoute
: attribut to target method of your middlewareData
: 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.