Selfrated.MinimalAPI.Middleware 1.0.1

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

// Install Selfrated.MinimalAPI.Middleware as a Cake Tool
#tool nuget:?package=Selfrated.MinimalAPI.Middleware&version=1.0.1                

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.

Reqiurements

and ASPNetCore applications, with at least .NET 7.0

Installation Instructions

dotnet add package Selfrated.MinimalAPI.Middleware

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;

[EndpointAPI]
public class Sample
{
    [EndpointMethod]
    public string TestEndpoint()
    {
        return "Hello World!";
    }
}

EndpointAPIAttribute

Any class that has wants to expose an endpoint will need to have this attribute defined.

Optional Parameters
  • Name - set this if you don't want to use the class name. Setting as null will exlude the path prefix (i.e. /Sample/TestEndpoint will become /TestEndpoint)
  • AuthenticationRequired - Default No. This will apply to all child methods, unless explicitly stated by the method
  • Roles - Array of role names required for authorization.
Sample with override
[EndpointAPI(Name = "NewPrefix", AuthenticationRequired = AuthenticationRequired.Yes)]

EndpointMethodAttribute

By using this on any method in a class that uses EndpointAPIAttribute, it will create an endpoint just like that!

Optional Parameters
  • Name - set this if you don't want to use the class name.
  • AuthenticationRequired - Default Inherit.
  • Roles - Array of role names required for authorization.
  • MethodType - Default Post. Can combine using bitwise OR operator (i.e MethodTypeEnum.Post | MethodTypeEnum.Get)
  • PrefixOverride - Override the url prefix from the parent.
  • EndpointFilter - Type that implements IEndpointFilter
Sample with parameters
[EndpointMethod(Name = "NewName", MethodType = MethodTypeEnum.POST | MethodTypeEnum.PUT)]

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.UseEndpoints();

    app.Run();
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.
  • net7.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.7 2,044 12/10/2022
1.0.6 305 12/10/2022
1.0.5 335 12/2/2022
1.0.4 344 11/29/2022
1.0.3 334 11/29/2022
1.0.2 336 11/29/2022
1.0.1 348 11/29/2022
1.0.0 331 11/29/2022