RestApiServer provides simple REST API based on EF DbContext. It is fast and lightweight and also easy to configure.
- Create ASP.NET Core 3.x application.
- Create entities and DbContext.
public class Customer
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public DateTimeOffset DateOfBirth { get; set; }
}
public class DemoDbContext : DbContext
{
public DemoDbContext(DbContextOptions<DemoDbContext> options) : base(options){}
public DbSet<Customer> Customers { get; set; }
}- Then configure it in
Startup.cs.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DemoDbContext>(options =>
{
options.UseInMemoryDatabase("memmory-db");
});- Install nuget package
install-package RestApiServer
- Configure RestApiServer
using RestApiServer;public void ConfigureServices(IServiceCollection services)
{
// Configure RestApiServer
services.AddRestApiServer<DemoDbContext>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Use RestApiServer middlewere
app.UseRestApiServer<DemoDbContext>();
}You can find full working example here.
Run web application. REST endpoints will be awailable in this pattern:
[HOST]/api/[DBSET_NAME]/[ID]
GET /api/customers
[
{
"id": 1,
"name": "John",
"dateOfBirth": "1995-05-05"
}
]GET /api/customers/1
{
"id": 1,
"name": "John",
"dateOfBirth": "1995-05-05"
}POST /api/customers
{
"name": "Jane",
"dateOfBirth": "1995-05-05"
}{
"id": 2,
"name": "Jane",
"dateOfBirth": "1995-05-05"
}PUT /api/customers/2
{
"id": 2,
"name": "Jane",
"dateOfBirth": "2000-05-05"
}{
"id": 2,
"name": "Jane",
"dateOfBirth": "2000-05-05"
}DELETE /api/customers/2