FGV.Lib.Api 1.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package FGV.Lib.Api --version 1.1.1
                    
NuGet\Install-Package FGV.Lib.Api -Version 1.1.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="FGV.Lib.Api" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FGV.Lib.Api" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="FGV.Lib.Api" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FGV.Lib.Api --version 1.1.1
                    
#r "nuget: FGV.Lib.Api, 1.1.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.
#:package FGV.Lib.Api@1.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FGV.Lib.Api&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=FGV.Lib.Api&version=1.1.1
                    
Install as a Cake Tool

FGV.Lib.Api

Introduction

FGV.Lib.Api is a comprehensive .NET library designed to standardize and simplify API development across FGV projects. It provides a set of reusable components, base classes, middleware, and extensions that enforce consistent patterns for building robust RESTful APIs.

This library handles common concerns such as:

  • Standardized API response formats
  • Error handling and logging
  • Authentication middleware
  • Swagger documentation setup
  • User context management

Key Components

ApiController

The ApiController is an abstract base class that extends the standard ASP.NET Core Controller. It provides helper methods for:

  • Creating standardized API responses
  • Handling success and error states uniformly
  • Retrieving authenticated user information
  • Consistent HTTP status code usage

Response Classes

CustomResult

CustomResult extends the standard JsonResult class to allow explicit HTTP status code setting. It ensures all API responses follow a consistent format.

Response<T>

The Response<T> class provides a standardized structure for all API responses, containing:

  • Status information
  • Error collections (when applicable)
  • Data payload of type T
  • Metadata for pagination or additional context

Middleware Components

BasicAuthMiddleware

A middleware that implements basic authentication for protecting specific endpoints, such as Swagger UI and Elmah error logs. It requires valid credentials configured in the application settings.

ErrorHandleMiddleware

A global error handling middleware that:

  • Captures unhandled exceptions
  • Logs error details
  • Returns standardized error responses
  • Prevents sensitive error information from leaking to clients

Swagger Extensions

The SwaggerSetup extension methods configure Swagger/OpenAPI documentation with:

  • Customizable API information
  • XML documentation integration
  • Authentication setup (JWT and Basic Auth)
  • Custom UI themes and styling

Installation

NuGet Package

dotnet add package FGV.Lib.Api

Project Reference

<ProjectReference Include="path_to_project\FGV.Lib.Api.csproj" />

Usage Examples

Program.cs Setup

var builder = WebApplication.CreateBuilder(args);

// Add FGV.Lib.Api services
builder.Services.AddControllers()
    .AddFGVApiServices();
    
// Configure Swagger using the library extension
builder.Services.AddSwaggerSetup("My API", "v1", "API documentation");

var app = builder.Build();

// Configure the middleware pipeline
app.UseMiddleware<ErrorHandleMiddleware>();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
    
    // Optional: Add basic auth protection for Swagger
    app.UseMiddleware<BasicAuthMiddleware>();
}

app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

app.Run();

Controller Implementation

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ApiController
{
    private readonly IProductService _productService;
    
    public ProductsController(IProductService productService)
    {
        _productService = productService;
    }
    
    [HttpGet]
    public async Task<IActionResult> GetAll()
    {
        var products = await _productService.GetAllAsync();
        return Success(products);
    }
    
    [HttpGet("{id}")]
    public async Task<IActionResult> GetById(int id)
    {
        var product = await _productService.GetByIdAsync(id);
        
        if (product == null)
            return NotFound("Product not found");
            
        return Success(product);
    }
    
    [HttpPost]
    public async Task<IActionResult> Create([FromBody] ProductDto product)
    {
        if (!ModelState.IsValid)
            return BadRequest(ModelState);
            
        var result = await _productService.CreateAsync(product);
        return Created(result);
    }
}

Dependencies

The library has the following dependencies:

  • .NET 7.0+
  • Microsoft.AspNetCore.Mvc
  • Microsoft.Extensions.DependencyInjection
  • Swashbuckle.AspNetCore (for Swagger integration)
  • Elastic.Apm.NetCoreAll (for APM monitoring)
  • Google.Cloud.PubSub.V1 (for event publishing)
  • Microsoft.AspNetCore.Authentication.JwtBearer (for JWT authentication)
  • Other FGV libraries:
  • FGV.Lib.Helpers
  • FGV.Lib.Log.ElmahCore
  • FGV.Lib.Persistence.SqlServer

License

Proprietary - FGV © 2023

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
1.5.0 127 7/1/2025
1.4.0 457 6/17/2025
1.3.0 198 6/5/2025
1.2.0 186 6/2/2025
1.1.2 141 6/2/2025
1.1.1 144 5/29/2025
1.1.0 141 5/29/2025
1.0.0 142 5/29/2025