RHttpServer 5.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package RHttpServer --version 5.1.0                
NuGet\Install-Package RHttpServer -Version 5.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RHttpServer" Version="5.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RHttpServer --version 5.1.0                
#r "nuget: RHttpServer, 5.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RHttpServer as a Cake Addin
#addin nuget:?package=RHttpServer&version=5.1.0

// Install RHttpServer as a Cake Tool
#tool nuget:?package=RHttpServer&version=5.1.0                

RedHttpServer

Low ceremony cross-platform http server framework with websocket support

.NET Core GitHub Nuget Nuget Dependent repos (via libraries.io)

A .NET Standard web application framework built on ASP.NET Core w/ Kestrel and inspired by the simplicity of Express.js

Installation

RedHttpServer can be installed from NuGet: Install-Package RHttpServer

Middleware and plugins

RedHttpServer is created to be easy to build on top of. The server supports both middleware modules and extension modules

  • JsonConverter - uses System.Text.Json
  • XmlConverter - uses System.Xml.Serialization
  • BodyParser - uses both the Json- and Xml converter to parse request body to an object, depending on content-type.

More extensions and middleware

Example

var server = new RedHttpServer(5000, "public");
server.Use(new EcsRenderer());
server.Use(new CookieSessions<MySession>(TimeSpan.FromDays(1)));

// Authentication middleware
async Task<HandlerType> Auth(Request req, Response res)
{
    if (req.GetData<MySession>() != null)
    {
        return HandlerType.Continue;
    }

    await res.SendStatus(HttpStatusCode.Unauthorized);
    return HandlerType.Final;
}

var startTime = DateTime.UtcNow;

// Url parameters
server.Get("profile/:username", Auth, (req, res) =>
{
    var username = req.Context.ExtractUrlParameter("username");
    // ... lookup username in database or similar and fetch profile ...
    var user = new { FirstName = "John", LastName = "Doe", Username = username };
    return res.SendJson(user);
});

// Using forms
server.Post("/login", async (req, res) =>
{
    var form = await req.GetFormDataAsync();
    // ... some validation and authentication ...
    await res.OpenSession(new MySession { Username = form["username"] });
    return await res.SendStatus(HttpStatusCode.OK);
});

server.Post("/logout", Auth, async (req, res) =>
{
    var session = req.GetData<MySession>();
    await res.CloseSession(session);
    return await res.SendStatus(HttpStatusCode.OK);
});

// Simple redirects
server.Get("/redirect", Auth, (req, res) => res.Redirect("/redirect/test/here"));

// File uploads
Directory.CreateDirectory("uploads");
server.Post("/upload", async (req, res) =>
{
    if (await req.SaveFiles("uploads"))
        return await res.SendString("OK");
    else
        return await res.SendString("Error", status: HttpStatusCode.NotAcceptable);
});

server.Get("/file", (req, res) => res.SendFile("somedirectory/animage.jpg"));

// Using url queries
server.Get("/search", Auth, (req, res) =>
{
    string searchQuery = req.Queries["query"];
    string format = req.Queries["format"];
    // ... perform search using searchQuery and return results ...
    var results = new[] { "Apple", "Pear" };

    if (format == "xml")
        return res.SendXml(results);
    else
        return res.SendJson(results);
});

// Markdown rendering
server.Get("/markdown", (req, res) => res.RenderFile("markdown.md"));

// Esc rendering
server.Get("/serverstatus", async (req, res) => await res.RenderPage("pages/statuspage.ecs",
    new RenderParams
    {
        { "uptime", (int) DateTime.UtcNow.Subtract(startTime).TotalSeconds },
        { "version", Red.RedHttpServer.Version }
    }));

// Using websockets
server.WebSocket("/echo", async (req, wsd) =>
{
    wsd.SendText("Welcome to the echo test server");
    wsd.OnTextReceived += (sender, eventArgs) => { wsd.SendText("you sent: " + eventArgs.Text); };
    return HandlerType.Final;
});

// Keep the program running easily (async Program.Main - C# 7.1+)
await server.RunAsync();

Why?

Because I like C# and .NET Core, but very often need a simple yet powerful web server for some project. Express.js is concise and simple to work with, so the API is inspired by that.

License

RedHttpServer is released under MIT license, so it is free to use, even in commercial projects.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on RHttpServer:

Package Downloads
Red.CookieSessions

Simple session management middleware for RedHttpServer. Uses cookies with authentication tokens

Red.JwtSessions

Package Description

Butterfly.Web.RedHttpServer

Implementation of Butterfly.Web for RedHttpServer (see https://github.com/rosenbjerg/Red)

Red.HandlebarsRenderer

Extension for Red for rendering Handlebars templates as responses

Red.EcsRenderer

Extension for Red for rendering .ecs files

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.2.1 11,326 3/16/2020 5.2.1 is deprecated because it is no longer maintained.
5.2.0 1,543 3/1/2020 5.2.0 is deprecated because it is no longer maintained.
5.1.0 3,060 2/22/2020 5.1.0 is deprecated because it is no longer maintained.
5.0.0 1,466 2/21/2020 5.0.0 is deprecated because it is no longer maintained.
4.0.2 13,462 9/23/2019 4.0.2 is deprecated because it is no longer maintained.
4.0.1 1,455 9/16/2019 4.0.1 is deprecated because it is no longer maintained.
4.0.0 4,766 4/29/2019 4.0.0 is deprecated because it is no longer maintained.
3.5.2 5,960 9/12/2018 3.5.2 is deprecated because it is no longer maintained.
3.5.1 1,649 9/7/2018 3.5.1 is deprecated because it is no longer maintained.
3.5.0 1,827 8/17/2018 3.5.0 is deprecated because it is no longer maintained.
3.4.0 1,729 8/15/2018 3.4.0 is deprecated because it is no longer maintained.
3.3.0 1,785 8/13/2018 3.3.0 is deprecated because it is no longer maintained.
3.2.2 1,841 8/6/2018 3.2.2 is deprecated because it is no longer maintained.
3.2.1 2,047 6/27/2018 3.2.1 is deprecated because it is no longer maintained.
3.2.0 2,687 6/26/2018 3.2.0 is deprecated because it is no longer maintained.
3.1.0 2,856 5/20/2018 3.1.0 is deprecated because it is no longer maintained.
3.0.0 5,778 3/26/2018 3.0.0 is deprecated because it is no longer maintained.
2.0.5 2,832 10/16/2017 2.0.5 is deprecated because it is no longer maintained.
2.0.4 1,924 10/16/2017 2.0.4 is deprecated because it is no longer maintained.
2.0.3 2,458 5/13/2017 2.0.3 is deprecated because it is no longer maintained.
2.0.2 2,517 4/27/2017 2.0.2 is deprecated because it is no longer maintained.
2.0.1 2,512 4/23/2017 2.0.1 is deprecated because it is no longer maintained.
2.0.0 2,540 4/12/2017 2.0.0 is deprecated because it is no longer maintained.
1.0.4.4 1,940 1/25/2017 1.0.4.4 is deprecated because it is no longer maintained.
1.0.4.3 2,008 1/24/2017 1.0.4.3 is deprecated because it is no longer maintained.
1.0.4.2 1,965 1/9/2017 1.0.4.2 is deprecated because it is no longer maintained.
1.0.4.1 2,221 12/12/2016 1.0.4.1 is deprecated because it is no longer maintained.
1.0.4 2,144 12/12/2016 1.0.4 is deprecated because it is no longer maintained.
1.0.3.9 1,957 12/9/2016 1.0.3.9 is deprecated because it is no longer maintained.
1.0.3.8 1,880 12/3/2016 1.0.3.8 is deprecated because it is no longer maintained.
1.0.3.7 2,259 11/8/2016 1.0.3.7 is deprecated because it is no longer maintained.
1.0.3.6 1,958 10/25/2016 1.0.3.6 is deprecated because it is no longer maintained.
1.0.3.5 1,881 10/13/2016 1.0.3.5 is deprecated because it is no longer maintained.
1.0.3.4 2,066 10/5/2016 1.0.3.4 is deprecated because it is no longer maintained.
1.0.3.3 1,971 9/27/2016 1.0.3.3 is deprecated because it is no longer maintained.
1.0.3.2 2,010 9/25/2016 1.0.3.2 is deprecated because it is no longer maintained.
1.0.3.1 2,085 9/22/2016 1.0.3.1 is deprecated because it is no longer maintained.
1.0.3 2,108 9/22/2016 1.0.3 is deprecated because it is no longer maintained.
1.0.2.11 2,219 9/17/2016 1.0.2.11 is deprecated because it is no longer maintained.
1.0.2.10 1,980 9/14/2016 1.0.2.10 is deprecated because it is no longer maintained.
1.0.2.9 1,900 9/14/2016 1.0.2.9 is deprecated because it is no longer maintained.
1.0.2.8 2,260 9/13/2016 1.0.2.8 is deprecated because it is no longer maintained.
1.0.2.7 1,876 9/12/2016 1.0.2.7 is deprecated because it is no longer maintained.
1.0.2.6 1,908 9/12/2016 1.0.2.6 is deprecated because it is no longer maintained.
1.0.2.5 1,913 9/12/2016 1.0.2.5 is deprecated because it is no longer maintained.
1.0.2.4 2,213 9/11/2016 1.0.2.4 is deprecated because it is no longer maintained.
1.0.2.3 1,869 9/11/2016 1.0.2.3 is deprecated because it is no longer maintained.

Add more start/run methods and fix runasync