No description
Find a file
ramboe 7a2081d25d
Update README.md
fixed another image
2023-09-03 12:10:05 +01:00
images added images for readme.md 2023-09-03 11:59:17 +01:00
Ramboe.MinimalApis added nuget support 2023-09-03 11:58:12 +01:00
Ramboe.MinimalApis.Testing init 2023-08-18 09:50:16 +01:00
.gitignore init 2023-08-18 09:50:16 +01:00
Ramboe.MinimalApis.sln init 2023-08-18 09:50:16 +01:00
README.md Update README.md 2023-09-03 12:10:05 +01:00

Ramboe.MinimalApis

Description

With this package you can outsource your asp.net core minimal api endpoints to dedicated classes to keep the Program.cs clean. It provides an interface IEndpoints that simply has to be implemented by your class:

When starting up the web service, all URLs of the IEndpoints are automatically mapped and all endpoints are supplied with the services from the Dependency Injection Container:

Installation

Create an empty ASP.NET Core project with the editor of your choice

should look something like this

Install the nuget package 'Ramboe.MinimalApis' and implement 'IEndpoints'

Within the ASP.NET Core Project, create a folder 'Endpoints' and inside that folder create a class 'ExampleEndpints.cs'

Then implement the 'IEndpoints' from the namespace 'Ramboe.MinimalApis'

using Ramboe.MinimalApis;

namespace WebApplication1.Endpoints
{
    public class ExampleEndpoints : IEndpoints
    {
        public static void DefineEndpoints(IEndpointRouteBuilder app)
        {
            app.MapGet("/", () => "hello from example endpoints");
            app.MapGet("/stuff", doStuff);
        }

        public static void AddServices(IServiceCollection services, IConfiguration configuration, bool isDev)
        {
            //services that these endpoints require
        }

        static IResult doStuff(HttpContext context)
        {
            var body = context.Request.Body.ToString();


            return Results.Ok(body);
        }
    }
}

Organize the startup

Inside Program.cs add the Endpoints to the services by using the folling lines

using Ramboe.MinimalApis;

var builder = WebApplication.CreateBuilder(args);

region insert
var isDev = builder.Environment.IsDevelopment();
builder.Services.AddEndpoints<Program>(builder.Configuration, isDev);
endregion

var app = builder.Build();

app.UseEndpoints<Program>(); //insert

app.Run();

The AddEndpoints() method makes the services from the dependency injection container (e.g. your DbContext) available to your IEndpoints.

The UseEndpoints() method executes the DefineEndpoints() method of each class that implements IEndpoints interface.

Test

Start your webservice to test if everything worked: