Getting Started
Installation
Using the SimpleW nuget package, always prefer the last version.
$ dotnet add package SimpleW
NOTE
SimpleW depends on Newtonsoft.Json package for json serialization/deserialization. It will be replaced in futur by the native System.Text.Json
as long as some advanced features will be covered (Populate
and streamingContextObject
, see work-in-progress).
REST API Example (backend)
The following example build a REST API with a clear routing and controller/method structured class.
using System;
using System.Net;
using SimpleW;
namespace Sample {
class Program {
static void Main() {
// listen to all IPs 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();
}
}
[Route("/test")]
public class TestController : Controller {
[Route("GET", "/hello")]
public object Hello(string? name = null) {
if (string.IsNullOrWhiteSpace(name)) {
return MakeNotFoundResponse("you must set a name parameter");
}
// the return will be serialized to json
return new {
message = $"{name}, Hello World !"
};
}
}
}
Then just open your browser to http://localhost:2015/api/test/hello?name=Chris and you will see the { "message": "Chris, Hello World !" }
json response.
Static Files Example (frontend)
The following example serve statics files in your c:\www
directory.
using System;
using System.Net;
using SimpleW;
namespace Sample {
class Program {
static void Main() {
// listen to all IPs port 2015
var server = new SimpleWServer(IPAddress.Any, 2015);
// serve static content located in your folder "C:\www\" to "/" endpoint
server.AddStaticContent(@"C:\www", "/");
// enable autoindex if no index.html exists in the directory
server.AutoIndex = true;
// start non blocking background server
server.Start();
Console.WriteLine("server started at http://localhost:2015/");
// block console for debug
Console.ReadKey();
}
}
}
<html>
<head>
<title>index</title>
</head>
<body>
<h1>Welcome<h1>
<p>Hello World !</p>
</body>
</html>
Then just point your browser to http://localhost:2015/.
FullStack Example (backend+frontend)
The following example will serve both static files in your c:\www
directory and a REST API.
using System;
using System.Net;
using SimpleW;
namespace Sample {
class Program {
static void Main() {
// listen to all IPs port 2015
var server = new SimpleWServer(IPAddress.Any, 2015);
// find all Controllers classes and serve on the "/api" endpoint
server.AddDynamicContent("/api");
// serve static content located in your folder "C:\www\" to "/" endpoint
server.AddStaticContent(@"C:\www", "/");
// start non blocking background server
server.Start();
Console.WriteLine("server started at http://localhost:2015/");
// block console for debug
Console.ReadKey();
}
}
[Route("/test")]
public class TestController : Controller {
[Route("GET", "/hello")]
public object Hello(string? name = null) {
if (string.IsNullOrWhiteSpace(name)) {
return MakeNotFoundResponse("you must set a name parameter");
}
// the return will be serialized to json
return new {
message = $"{name}, Hello World !"
};
}
}
}
<html>
<head>
<title>index</title>
</head>
<body>
<h1>Welcome<h1>
<input type="text" id="nameInput" placeholder="Enter your name" />
<button id="greetBtn">Greet me</button>
<p id="hello"></p>
</body>
<script>
document.addEventListener('DOMContentLoaded', () => {
const api = "/api";
const input = document.getElementById('nameInput');
const button = document.getElementById('greetBtn');
const output = document.getElementById('hello');
button.addEventListener('click', () => {
const name = input.value.trim();
if (!name) {
output.textContent = 'Please enter a name.';
return;
}
fetch(`/test/hello?name=${encodeURIComponent(name)}`)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
output.textContent = data.message || 'No message in response';
})
.catch(err => {
console.error('Fetch error:', err);
output.textContent = 'Failed to fetch greeting.';
});
});
});
</script>
</html>
Then just point your browser to http://localhost:2015/ and interact with the form.