AspNetForge.Web
1.2.1
dotnet add package AspNetForge.Web --version 1.2.1
NuGet\Install-Package AspNetForge.Web -Version 1.2.1
<PackageReference Include="AspNetForge.Web" Version="1.2.1" />
<PackageVersion Include="AspNetForge.Web" Version="1.2.1" />
<PackageReference Include="AspNetForge.Web" />
paket add AspNetForge.Web --version 1.2.1
#r "nuget: AspNetForge.Web, 1.2.1"
#:package AspNetForge.Web@1.2.1
#addin nuget:?package=AspNetForge.Web&version=1.2.1
#tool nuget:?package=AspNetForge.Web&version=1.2.1
AspNetForge.Web
Generic extension methods for ASP.NET Core Minimal API with Entity Framework Core.
Overview
AspNetForge.Web provides a set of generic extension methods that register standard CRUD (Create, Read, Update, Delete) endpoints for Entity Framework Core entities in ASP.NET Core Minimal API applications.
Installation
dotnet add package AspNetForge.Web
Dependencies
Microsoft.EntityFrameworkCore(>= 8.0.0)Npgsql.EntityFrameworkCore.PostgreSQL(>= 9.0.4)Microsoft.AspNetCore.Identity.EntityFrameworkCore(>= 8.0.27)
Usage
Define your models and DbContext
public class Product
{
public int Id { get; set; }
public required string Name { get; set; }
public required string Article { get; set; }
public required decimal MinCost { get; set; }
}
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<Product> Products { get; set; }
}
Register endpoints in Program.cs
using AspNetForge.Web.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
app.UseDefaultFiles();
app.UseStaticFiles();
// Register CRUD endpoints for each entity
app.MapCrud<Product, AppDbContext>("/api/products");
app.Run();
This single line registers five endpoints:
| Method | Route | Description |
|---|---|---|
| GET | /api/products |
Get all entities |
| GET | /api/products/{id} |
Get entity by ID |
| POST | /api/products |
Create new entity |
| PUT | /api/products/{id} |
Update entity by ID |
| DELETE | /api/products/{id} |
Delete entity by ID |
Custom endpoints
MapCrud covers the standard operations. For custom logic, add endpoints manually as usual:
app.MapCrud<Product, AppDbContext>("/api/products");
// Custom endpoint alongside generated CRUD
app.MapGet("/api/products/search", async (string query, AppDbContext db) =>
await db.Products.Where(p => p.Name.Contains(query)).ToListAsync());
JavaScript REST client
The package includes an embedded CrudClient JavaScript module for interacting with the registered endpoints from the browser.
Extract it to wwwroot/js/ in Program.cs:
app.UseForgeResources();
app.UseDefaultFiles();
app.UseStaticFiles();
Then reference it in your HTML:
<script src="/js/crud.js"></script>
<script>
const api = new CrudClient('/api/products');
// GET all
const products = await api.getAll();
// POST
const created = await api.create({ name: 'Item', article: 'A-01', minCost: 100 });
// PUT
const updated = await api.update(1, { name: 'Updated', article: 'A-01', minCost: 150 });
// DELETE
await api.remove(1);
</script>
API Reference
MapCrud<TEntity, TContext>(this WebApplication app, string route)
Registers GET (all), GET (by id), POST, PUT, DELETE endpoints for the given entity type.
Type parameters:
TEntity— Entity class with anint IdpropertyTContext—DbContextclass containing aDbSet<TEntity>
Parameters:
route— Base route path (e.g."/api/products")
Returns: WebApplication (for chaining)
UseForgeResources(this WebApplication app, bool overwrite = false)
Extracts embedded JavaScript resources to wwwroot/js/.
CrudClient (JavaScript)
Client-side class for REST API interaction via Fetch API.
| Method | Returns | Description |
|---|---|---|
getAll() |
Promise<T[]> |
GET all entities |
getById(id) |
Promise<T\|null> |
GET entity by ID |
create(entity) |
Promise<T> |
POST new entity |
update(id, entity) |
Promise<T\|null> |
PUT update entity |
remove(id) |
Promise<boolean> |
DELETE entity |
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.AspNetCore.Identity.EntityFrameworkCore (>= 8.0.27)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 9.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.