Skip to content

A simple web server using latest dotNet runtime with ASP.NET Core like grammer that can running on any platforms!

License

Notifications You must be signed in to change notification settings

Corona-Studio/SharpWebServer

Repository files navigation

SharpWebServer

Description

This is a simple web server framework power by .NET 6 using HttpListener.
It has a syntax similar to AspNet Core.
You can use it to run a simple webserver on any platform without Server bundle!

Features

Feature Status
Http Get
Http Post
Custom Http Method
Multiple Controllers
Middlewares (Handlers)
JSON return value
Acquire arguments from body
Acquire arguments from form
Acquire arguments from query
Acquire arguments from route
HTTPS support WIP

Usage

You can take a look at SharpWebServerDemo project for the example code.

var webServer = new SimpleWebServer("http://[BIND_ADDRESS]:[BIND_PORT]/");

webServer.RegisterHandler<LogHandler>();          // normal handler
webServer.RegisterHandler<ErrorHandler>();        // error handler

webServer.RegisterController<ControllerDemo>();   // register you controller

webServer.Start();                                // start!

Add a controller

[ApiController]                                                                         // add ApiController attribute
public class ControllerDemo : Controller
{
    [HttpGet(Path="test/test/{x}")]                                                     // define HTTP method
    public IActionResult Test([FromRoute]int x, [FromBody] IEnumerable<string> texts)   // define your arguments
    {
        Console.WriteLine($"{x} - Test, {string.Join(',', texts)}!");

        return ServiceUnavailable(new                                                   // define status code and the response content!
        {
            info = "xie!"
        });
    }
}

Add a handler

public class LogHandler : IHandler
{
    public void Handle(HttpListenerContext context)
    {
        // your code goes here
    }
}

About

A simple web server using latest dotNet runtime with ASP.NET Core like grammer that can running on any platforms!

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages