WebServerLight 0.0.24-beta-24

This is a prerelease version of WebServerLight.
There is a newer version of this package available.
See the version list below for details.
dotnet add package WebServerLight --version 0.0.24-beta-24
                    
NuGet\Install-Package WebServerLight -Version 0.0.24-beta-24
                    
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="WebServerLight" Version="0.0.24-beta-24" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WebServerLight" Version="0.0.24-beta-24" />
                    
Directory.Packages.props
<PackageReference Include="WebServerLight" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WebServerLight --version 0.0.24-beta-24
                    
#r "nuget: WebServerLight, 0.0.24-beta-24"
                    
#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.
#:package WebServerLight@0.0.24-beta-24
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=WebServerLight&version=0.0.24-beta-24&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=WebServerLight&version=0.0.24-beta-24&prerelease
                    
Install as a Cake Tool

WebServerLight

A C# light weight web server

Table of contents

  1. Setup
  2. Configuration
    1. Enabling HTTP and/or HTTPS
    2. Certificate for HTTPS
    3. Serving a web site from .NET resource
    4. Use range requests for streaming media files like videos
  3. Routing
  4. Serving requests
  5. WebSockets

Setup <a name="setup"></a>

In a C# Console program, you have to add this package to your project:

<ItemGroup>
    <PackageReference Include="WebServerLight" Version="0.0.23-beta-23" />
</ItemGroup>

Now you can set up your web server with the builder pattern:

var server =
    WebServer
        .New()
        ...
        .Build();

server.Start();
ReadLine();
server.Stop();

Configuration <a name="configuration"></a>

The Web Server can be configured with the help of many configuration functions

Enabling HTTP and/or HTTPS <a name="httpenabling"></a>

Insecure HTTP can be enabled like this:

var server =
    WebServer
        .New()
        .Http()
        .Build();

or specifying another HTTP port:

WebServer
    .New()
    .Http(8080)

Secure HTTPS can be enabled like this:

var server =
    WebServer
        .New()
        .Https(4433)
        .Build();

Certificate for HTTPS <a name="certificate"></a>

There are two ways for providing a HTTPS Certificate:

  1. Configuring a .pfx file containig the certificate
  2. Automatically requesting a certificate via Let's Encrypt

If you have a valid pfx certificate with private key inxcluded, you can configure the web server to use this certificate:

var server =
    WebServer
        .New()
        .Https(4433)
        .HttpsCertificate("<path to .pfx file>")
        .Build();

To request a certificate via Let's Encrypt you can use the nuget tool https://www.nuget.org/packages/LetsencryptCert .When correctly configured (see README of this tool), it will automatically request a new valid certificate before the old certificate will be invalid. Let's Encrypt will call your website via HTTP to check the domain, so HTTP has to be enabled. All you have to do in your web server besides configuring LetsencryptCert:

var server =
    WebServer
        .New()
        .Http()
        .Https()
        .UseLetsEncrypt()
        .Build();

Serving a web site from .NET resource <a name="websitefromresource"></a>

WebServerLight can host a complete web site and other resources from files which are included as .NET resources. You have to include the files in your .csproj project file like this:

<ItemGroup>
    <EmbeddedResource Include="./website/index.html">
      <LogicalName>/index.html</LogicalName>
    </EmbeddedResource>
    <EmbeddedResource Include="./website/css/styles.css">
      <LogicalName>/css/styles.css</LogicalName>
    </EmbeddedResource>
    <EmbeddedResource Include="./website/scripts/script.js">
      <LogicalName>/scripts/script.js</LogicalName>
    </EmbeddedResource>
    <EmbeddedResource Include="./website/images/image.jpg">
      <LogicalName>image</LogicalName>
    </EmbeddedResource>
  </ItemGroup>

The LogicalName is at the same time the resource part of your URL. /index.html does not have to be specified.

To activate this you have to call WebsiteFromResource():

var server =
    WebServer
        .New()
        .Http()
        .WebsiteFromResource()
        .Build();

Use range requests for streaming media files like videos <a name="userange"></a>

If you want to stream video or audio files, all you have to do is activating it with the help of the method UseRange():

var server =
    WebServer
        .New()
        .Http()
        .WebsiteFromResource()
        .UseRange()
        .Build();

When the web site requests a range request, the media file will be streamed.

Routing <a name="routing"></a>

WebServerLight has a routing concept. You can add new routes with the help of the function Route:

var server =
    WebServer
        .Route()

Threre are several route-Objects you can include:

  • HttpRoute (only called when using http:// scheme)
  • HttpsRoute (only called when using https:// scheme)
  • MethodRoute (only called when the HTTP method is one of GET, PUT, POST, ...)
  • PathRoute (only called when the URL path part starts with the as parameter given path)
  • PathExactRoute (only called when the URL path part equals the as parameter given path)

These routes are included with the help of the New() static function like this:

var server =
    WebServer
        .Route(MethodRoute
            .New(Method.Get))

Here you add a new route that is only called when the HTTP method is GET.

The routes can be combined like this:

var server =
    WebServer
        .Route(MethodRoute
            .New(Method.Get))
                .Add(PathRoute
                        .New("/image")
                        .Request(GetImage))
                .Add(PathRoute
                        .New("/video")
                        .Request(GetVideo)))

When the method is GET and when the path starts with /image, then the request GetImage is being called. Otherwise when the method is GET and when the path starts with /video, then the request GetImage is being called.

Serving requests <a name="servingrequests"></a>

Like you have seen in the previous section, a request is being served with the call to Request. Request has the following signature:

public Route Request(Func<IRequest, Task<bool>> request);

You include a request function as parameter which is called when all routing conditions are OK for the specific HTTP request. The routing function has a parameter of type IRequest and retuns asyncronously a boolean result. If it is true, te request is being served. If it is false, the next route is being probed.

// TODO examples of string, stream, json request functions

WebSockets <a name="websockets"></a>

...

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 57 8/3/2025
1.0.3 148 6/19/2025
1.0.2 146 6/18/2025
1.0.1 286 6/12/2025
1.0.0 281 6/10/2025
0.0.25-beta-25 279 6/10/2025
0.0.24-beta-24 279 6/10/2025
0.0.23-beta-23 258 6/9/2025
0.0.22-beta-22 141 6/5/2025
0.0.20-beta-21 137 6/5/2025
0.0.20-beta-20 134 6/4/2025
0.0.19-beta-19 133 6/4/2025
0.0.18-beta-18 137 6/3/2025
0.0.17-beta-17 134 6/2/2025
0.0.16-beta-16 138 6/2/2025
0.0.15-beta-15 116 2/16/2025
0.0.14-beta-14 106 2/16/2025
0.0.13-beta-13 92 2/15/2025
0.0.12-beta-12 96 2/15/2025
0.0.11-beta-11 86 2/15/2025
0.0.10-beta-10 96 2/15/2025
0.0.9-beta-9 99 2/14/2025
0.0.8-beta-8 122 2/12/2025
0.0.7-beta-7 104 2/12/2025
0.0.6-beta-6 106 2/11/2025
0.0.5-beta-5 97 2/10/2025
0.0.4-beta-4 101 2/10/2025
0.0.3-beta-3 103 2/10/2025
0.0.2-beta-2 100 2/10/2025
0.0.1-beta-1 102 2/9/2025