Skip to content

adambarclay/mundane-hosting-aspnet

Repository files navigation

Mundane.Hosting.AspNet

License: MIT nuget build coverage

Mundane is a lightweight "no magic" web framework for .NET.

This package enables a Mundane application to be hosted with ASP.NET.

See the Mundane documentation for more information.

Getting Started

Install the Mundane.Hosting.AspNet nuget package, then in your ASP.NET startup code call app.UseMundane(); passing in the routing and dependencies configuration.

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        var dependencies = new Dependencies(
            new Dependency<Configuration>(new Configuration(env)),
            new Dependency<DataRepository>(request => new DataRepositorySqlServer(
                request.Dependency<Configuration>().ConnectionString)));

        var routing = new Routing(
            routeConfiguration =>
            {
                routeConfiguration.Get("/", HomeController.HomePage);
                routeConfiguration.Get("/data/{id}", DataController.GetData);
                routeConfiguration.Post("/data/{id}", DataController.UpdateData);
            });

        app.UseMundane(dependencies, routing);
    }

Executing Requests

Endpoints can be executed in a different part the ASP.NET pipeline by calling MundaneMiddleware.ExecuteRequest(). For example you may want to do custom error handling while still making use of the Mundane engine.

Passing the current HttpContext and the routing and dependencies configuration will execute the endpoint which matches the request.

    public static async ValueTask ExecuteRequest(
        HttpContext context,
        DependencyFinder dependencyFinder,
        Routing routing)

It is also possible to execute a specifc endpoint with:

    public static async ValueTask ExecuteRequest(
        HttpContext context,
        DependencyFinder dependencyFinder,
        MundaneEndpoint endpoint,
        Dictionary<string, string> routeParameters)

The endpoint must be a MundaneEndpoint which has the signature ValueTask<Response> Endpoint(Request request). Any of the other Mundane endpoint signatures can be converted to a MundaneEndpoint by calling MundaneEndpointFactory.Create() e.g.

    MundaneEndpointFactory.Create(() => Response.Ok(o => Write("Hello World!")));

Since there is no routing information in this version of ExecuteRequest(), you must also supply an appropriate routeParameters dictionary for the endpoint. When called as part of the pipeline, Mundane creates a dictionary of parameters captured from the URL, e.g. for the route /my-endpoint/{id}, called with /my-endpoint/123, Mundane passes new Dictionary<string, string> { { "id", "123" } } as routeParameters.

If the endpoint does not require route parameters, pass an empty dictionary: new Dictionary<string, string>(0);.

About

Mundane is a lightweight "no magic" web framework for .NET. This package enables a Mundane application to be hosted with ASP.NET.

Topics

Resources

License

Stars

Watchers

Forks

Languages