WebOcelot 1.0.1

dotnet add package WebOcelot --version 1.0.1                
NuGet\Install-Package WebOcelot -Version 1.0.1                
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="WebOcelot" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WebOcelot --version 1.0.1                
#r "nuget: WebOcelot, 1.0.1"                
#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 WebOcelot as a Cake Addin
#addin nuget:?package=WebOcelot&version=1.0.1

// Install WebOcelot as a Cake Tool
#tool nuget:?package=WebOcelot&version=1.0.1                

Ocelot

WebOcelot is a dependency-free, minimal MVC web framework designed for .NET, optimized for Ahead-Of-Time (AOT) compilation.

Why

  • ASP .NET Core does not support MVC with AOT.
  • It was fun writing everything from scratch.

Running an app

To run a web application:

  1. Initialize the HttpServer with host and port.
  2. Register controller routes using RegisterRoutes.
  3. Optionally, configure static file serving and template paths.
using Ocelot.Rendering.Models;
using Ocelot.Responses;
using Ocelot.Structures;
using Ocelot.Web;

var app = new HttpServer("127.0.0.1", 8080);

app.RegisterRoutes<HomeController>();
app.UseStaticFiles("wwwroot");
app.UseTemplatePath("Views");

app.Run();

Templating

A no logic, variable only template engine is included for HTML views.

Passing a ViewModel from the controller like this:

// ...
public class HomeController : Controller
{
    [Get("/")]
    public static View Index()
    {
        return Render(
            new ViewModel
            {
                { "Firstname", "Bill" },
                { "Lastname", "Gates" },
                { "Something", "Something else" }
            },
            "index"
        );
    }
// ...

Its values can then be accessed from index.html in the Views directory:

<html>
{{Firstname}} {{Lastname}}
</html>

Obviously, other more convoluted templating engines like Liquid, etc... can also be used. Though, they will void AOT compatibility.

Project Structure

  • Controllers: Define routes and logic in controllers by inheriting from Ocelot.Web.Controller.
  • Views: Use no-logic templates to render HTML, stored in the path specified with UseTemplatePath.
  • Static Files: Serve static content like CSS, JS, images, etc using UseStaticFiles.

Example Usage

using Ocelot.Rendering.Models;
using Ocelot.Responses;
using Ocelot.Structures;
using Ocelot.Web;

namespace Ocelot.Test
{
    public class HomeController : Controller
    {
        [Get("/")]
        public static View Index()
        {
            return Render(
                new ViewModel
                {
                    { "Firstname", "Bill" },
                    { "Lastname", "Gates" },
                    { "Something", "Something else" }
                },
                "index"
            );
        }

        [Post("/submit")]
        public static Text Submit(HttpRequest request) => new($"Received: {request.Body}");

        [Get("/json")]
        public static Json SerialExample() => new("{\"message\": \"This is a JSON response\"}");

        [Get("/about")]
        public static Text About() => new("This is the about page.");

        [Get("/increment/{number}")]
        public static Text Increment(string value) => new($"Incremented: {int.Parse(value) + 1}");

        [Get("/greet/{entity}/{name}")]
        public static Text Greet(string entity, string name) => new($"Hello {name}, the {entity}");
    }

    public class MainTests
    {
        [Test]
        public void ScaffoldingTest()
        {
            var app = new HttpServer("127.0.0.1", 8080);
            app.RegisterRoutes<HomeController>();
            app.UseStaticFiles("Static");
            app.UseTemplatePath("Views");
            Assert.Pass();
        }
    }
}
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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

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.1 117 8/29/2024