Basic
SimpleW covers the most common web server use cases — from minimal code setups to full-stack applications.
Minimal Example
The following minimal example can be used for rapid prototyping.
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);
// minimal api
server.MapGet("/api/test", () => {
return new { message = "Hello World !" };
});
// start non blocking background server
server.Start();
Console.WriteLine("server started at http://localhost:2015/");
// block console for debug
Console.ReadKey();
}
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
It uses the MapGet and MapPost methods, which are quite the same as asp.net core.
Rest API Example
The following example build a REST API with a clear routing and controller/method structured class.
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();
}
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System.Net;
using SimpleW;
namespace Sample {
// inherit from Controller
public class SomeController : Controller {
// use the Route attribute to target a public method
[Route("GET", "/test")]
public object SomePublicMethod(string name = "World") {
// Query param passed in through function params
// 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(string name) {
return $"Hello {name} !";
}
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Then just open your browser to http://localhost:2015/api/test/hello?name=Chris and you will see the { "message": "Hello Chris !" } 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 controllers.
Static Files Example
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, cached for 24h
server.AddStaticContent(@"C:\www", "/", timeout: TimeSpan.FromDays(1));
// 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();
}
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<html>
<head>
<title>index</title>
</head>
<body>
<h1>Welcome<h1>
<p>Hello World !</p>
</body>
</html>2
3
4
5
6
7
8
9
Then just point your browser to http://localhost:2015/.
FullStack Example
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, cached for 24h
server.AddStaticContent(@"C:\www", "/", timeout: TimeSpan.FromDays(1));
// start non blocking background server
server.Start();
Console.WriteLine("server started at http://localhost:2015/");
// block console for debug
Console.ReadKey();
}
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Net;
using SimpleW;
namespace Sample {
[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 !"
};
}
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<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 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(`/api/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>2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Then just point your browser to http://localhost:2015/ and interact with the form.
NOTE
You frontend can be anything, you can serve a vue/react/whatever javascript application you want. In fact, for my needs, I have to serve a frontend vuetify application.
See the How to serve a vue.js app with SimpleW.
