FGV.Lib.Api
1.1.1
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
<PackageReference Include="FGV.Lib.Api" Version="1.1.1" />
<PackageVersion Include="FGV.Lib.Api" Version="1.1.1" />
<PackageReference Include="FGV.Lib.Api" />
paket add FGV.Lib.Api --version 1.1.1
#r "nuget: FGV.Lib.Api, 1.1.1"
#:package FGV.Lib.Api@1.1.1
#addin nuget:?package=FGV.Lib.Api&version=1.1.1
#tool nuget:?package=FGV.Lib.Api&version=1.1.1
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 | Versions 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. |
-
net7.0
- Elastic.Apm.NetCoreAll (>= 1.32.2)
- FGV.Lib.Helpers (>= 1.0.0)
- FGV.Lib.Log.ElmahCore (>= 1.0.0)
- FGV.Lib.Persistence.SqlServer (>= 1.0.0)
- Google.Cloud.PubSub.V1 (>= 3.24.0)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 7.0.14)
- Swashbuckle.AspNetCore (>= 8.1.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.