AspNetCore.MinimalApi.Ext 3.0.0-alpha

This is a prerelease version of AspNetCore.MinimalApi.Ext.
There is a newer version of this package available.
See the version list below for details.
dotnet add package AspNetCore.MinimalApi.Ext --version 3.0.0-alpha                
NuGet\Install-Package AspNetCore.MinimalApi.Ext -Version 3.0.0-alpha                
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="AspNetCore.MinimalApi.Ext" Version="3.0.0-alpha" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AspNetCore.MinimalApi.Ext --version 3.0.0-alpha                
#r "nuget: AspNetCore.MinimalApi.Ext, 3.0.0-alpha"                
#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 AspNetCore.MinimalApi.Ext as a Cake Addin
#addin nuget:?package=AspNetCore.MinimalApi.Ext&version=3.0.0-alpha&prerelease

// Install AspNetCore.MinimalApi.Ext as a Cake Tool
#tool nuget:?package=AspNetCore.MinimalApi.Ext&version=3.0.0-alpha&prerelease                

Minimal API Middleware

A set of tools to simplify creating ASPNetCore applications, specifically when using MinimalAPIs.

The middleware helps clean up your code by making it easy to break the application startup into seperate classes, ideally named by what their purpose is.

Why forked ?

  • Code is cleaned up and simplified.
  • Added support for custom attributes.
  • Seperated attributes as a better design choice.
  • Better namings
  • Forced single endpoint per class.

Reqiurements

ASPNetCore applications, with at least .NET 7.0

Installation Instructions

This is a fork from original project there is no package yet. You can only install it from github.

Endpoint Attributes

By utilizing these attributes, you can quickly and easily get endpoints created from any file that uses them. By default the names of the classes/methods will be the names of the endpoints, requiring as little code/effort as possible.

This is a fully functional file that will create a fully functional endpoint for /Sample/TestEndpoint ( i.e. https://localhost:7000/Sample/TestEndpoint)!

using Selfrated.MinimalAPI.Middleware.Attributes;

namespace WebApplication1.Endpoints;


public class Sample : BaseEndpoint
{
    public string Handle(HttpContext context)
    {
        return "Hello World!";
    }   
}

Every class that has BaseEndpoint as a parent will automatically be processed for creating endpoints. Only Handle method will be considered as an endpoint.

Library forces you to have single endpoint per class. You can create multiple classes in a single file but not 2 or more endpoints in a single class.

EndpointAuthorizeAttribute

By using this on class, it will require authorization to access the endpoint.

EndpointFilterAttribute

By using this on class, you can use custom filters that assigned from IEndpointFilter

EndpointRouteAttribute

By using this on class, you can override the route of the endpoint.

EndpointHttpMethodAttribute

By using this on class, you can override the http method of the endpoint. You can use multiple and if you use none it will be GET by default.

Why not using default attributes ?

Currently default attributes provided by AspNetCore is not supported but support can be added easily.

Custom attributes provided by the library currently only way to go and can only be used on the class not method.

EndpointMiddlewareOptions

You can use EndpointMiddlewareOptions to configure the middleware.

app.UseMinimalApiEndpoints(x => { 
  x.GlobalPrefix = "api"; //Url: /api/Product/Get etc.

  //Global filters
  x.EndpointFilters = new List<Type> { typeof(CustomFilter) };

  //Global authorization
  x.AuthorizeData = new AuthorizeData(){
    Policy = "PolicyName"
  };
});

What is not supported ?

  • Currently only class level custom attributes are supported.
  • Model binding to query is not supported however you can use FromQueryAttribute to bind to query.
  • IActionFilters etc. are not supported.
  • Swagger generation with folders as controllers

IBuilderServiceSetup and IApplicationSetup

Any objects that implement IBuilderServiceSetup and/or IApplicationSetup will be processed when the WebApplication is built. This happens once, when the applcation starts.

The following sample file (Authentication.cs) sets up azure AD authentication with the application.

public class Authentication : IApplicationSetup, IBuilderServiceSetup
{
    public void InitializeApplication(WebApplication app)
    {
        app.UseAuthentication();
        app.UseAuthorization();
    }

    public void InitializeServices(IServiceCollection services, ConfigurationManager configuration, ConfigureHostBuilder host)
    {
        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApi(options =>
        {
            configuration.Bind("AzureAd", options);
        },
        options => { configuration.Bind("AzureAd", options); });
    }
}

Setup (Program.cs)

This is a complete Program.cs file!

    var builder = WebApplication.CreateBuilder(args);

    //if using IBuilderServiceSetup
    builder.UseBuilderSetup();

    var app = builder.Build();

    //if using IApplicationSetup
    app.UseApplicationSetup();

    //if using EndpointAttributes (Minimal API)
    app.UseMinimalApiEndpoints();

    app.Run();

Changelogs

3.0.0
  • BuilderSetup changed so that it only asks for WebApplicationBuilder
  • Added new attributes for customizing endpoints methods EndpointHttpGet etc.
  • Added built-in support for better swagger generation. With few changes you can generate swagger with folders as controllers.
  • Added new methods that adds Endpoint options as singleton
2.0.0
  • Project forked and cleaned up.
  • Added support for custom attributes.
  • Seperated attributes as a better design choice.
  • Better namings
  • Forced single endpoint per class.
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
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
3.2.2 176 8/15/2023
3.2.1-beta 130 8/15/2023
3.1.0-alpha 129 8/15/2023
3.0.1-alpha 123 8/14/2023
3.0.0-alpha 114 8/14/2023