AutoController 1.0.10
See the version list below for details.
dotnet add package AutoController --version 1.0.10
NuGet\Install-Package AutoController -Version 1.0.10
<PackageReference Include="AutoController" Version="1.0.10" />
paket add AutoController --version 1.0.10
#r "nuget: AutoController, 1.0.10"
// Install AutoController as a Cake Addin #addin nuget:?package=AutoController&version=1.0.10 // Install AutoController as a Cake Tool #tool nuget:?package=AutoController&version=1.0.10
Autocontroller
<a href="https://www.nuget.org/packages/AutoController"> <img alt="Nuget (with prereleases)" src="https://img.shields.io/nuget/vpre/Autocontroller"> </a> <a href="https://www.nuget.org/packages/AutoController"> <img alt="Nuget" src="https://img.shields.io/nuget/dt/AutoController"> </a>
Automaticly create REST API endpoints from your Entity Framework Core database context. CRUD actions becomes more easy!
Supported features:
<ul> <li>Pagination by default</li> <li>Sorting results</li> <li>Interacting type can be JSON or XML</li> <li>Flexible and adjustable request/response schema</li> <li>Filtering results</li> <li>Authentificated Access and Permitions Control</li> <li>Execute some Actions before save/delete</li> <li>Execute some Actions before DbContext SaveChanges/SaveChangesAsync</li> </ul>
Routers and handlers for paths will be created: <ul> <li>"/Your Entity/Index" GET</li> <li>"/Your Entity/Count" GET</li> <li>"/Your Entity/Save" POST</li> <li>"/Your Entity/Update" PUT</li> <li>"/Your Entity/Delete" DELETE</li> </ul> Interacting type can be JSON or XML.
Database supported: <ul> <li>SQLLite</li> <li>SQLServer</li> <li>PostgreSQL</li> </ul>
How to use
Install package via nugget
dotnet add package AutoController
Mark your entity type as a member of AutoController by using Attribute
[MapToController(
// name & adress of controller
"Blogs")]
Create some restrictions, if needed These attributes inherits from Authorize attribute and woks the same. It can be combined
// GET access restriction
[GetRestriction(Roles = "Administrator")]
// POST access restriction
// user must have Administrator role
[PostRestriction(Roles = "Administrator")]
// DELETE access restriction
[DeleteRestriction(Roles = "Administrator")]
Change your Startup.cs configuration file as follows:
using AutoController;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// ---- Your code -----------//
// Register Autocontroller in default DI container
// you should describe database type and connection string here!
var connString = Configuration.GetConnectionString("DefaultConnection");
services.AddAutoController<ApplicationDBContext>(DatabaseTypes.SQLite, connString);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ---- Your code -----------//
// Create JsonSerializerOptions object if you use JSON interacting method
var JsonOptions = new JsonSerializerOptions
{
WriteIndented = true
};
// Create your api
// handle requests for api/<your entity>/... paths with Json
app.UseAutoController<ApplicationDBContext>("api", true, InteractingType.JSON, JsonOptions);
// handle requests for apixml/<your entity>/... paths with XML
app.UseAutoController<ApplicationDBContext>("apixml", true, InteractingType.XML);
// handle requests for apijson/<your entity>/... paths with Json
app.UseAutoController<ApplicationDBContext>("apijson", true, null, JsonOptions);
// ----- With options------------------
var options = new AutoControllerOptions() {
JsonSerializerOptions = JsonOptions,
LogInformation = true,
RoutePrefix="api4",
AuthentificationPath = "/login",
AccessDeniedPath = "/anauthorized"
};
app.UseAutoController<ApplicationDBContext>(options);
}
}
When Your Application has been started, You can see these responses:
GET Requests https://localhost:5001/api/Blogs/Index - JSON Result https://localhost:5001/apixml/Blogs/Index - XML Result
https://localhost:5001/api/Blogs/Index?page=1&size=5 - JSON Result with first 5 items https://localhost:5001/api/Blogs/Index?page=1&size=5&sort=Content - JSON Result with first 5 items, ordered by Content
Filtering results: for filtering results, the library https://dynamic-linq.net/ has been used. https://localhost:5001/api/Blogs/Index?filter=id = 1 - JSON Result with item with id = 1 https://localhost:5001/api/Blogs/Index?filter=Content!=null and Subject !=null - JSON Result with items with fiter conditions
POST Requests https://localhost:5001/api/Blogs/Save - Save Blog into Your Database. Blog will be recived from request body
PUT Requests https://localhost:5001/api/Blogs/Update - Update Blog into Your Database. Blog will be recived from request body
DELETE Requests https://localhost:5001/api/Blogs/Delete - Remove Blog from Your Database. Blog will be recived from request body
Handle some actions before save, and delete object:
When You need do something before save and delete objects, You can implement interfaces The method DoBeforeSave has been called before save object. It returns true when object can be saved and deleted or false, when not. Save and update requests and restrictions works the same. It returns reason text with responce.
/// <summary>
/// Implement this interface in your entity types
/// </summary>
public interface IActionBeforeSave<T> where T:DbContext
{
/// <summary>
/// Do something before save
/// </summary>
/// <param name="dbcontext">DbContext</param>
/// <param name="reason">The reason why the object cannot be saved</param>
public bool DoBeforeSave(T dbcontext, out string reason);
}
/// <summary>
/// Implement this interface in your entity types
/// </summary>
public interface IActionBeforeDelete<T> where T:DbContext
{
/// <summary>
/// Do something before delete
/// </summary>
/// <param name="dbcontext">DbContext</param>
/// <param name="reason">The reason why the object cannot be removed</param>
public bool DoBeforeDelete(T dbcontext, out string reason);
}
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. |
-
net8.0
- Microsoft.AspNetCore.Authorization (>= 8.0.8)
- Microsoft.AspNetCore.Identity.EntityFrameworkCore (>= 8.0.8)
- Microsoft.EntityFrameworkCore (>= 8.0.8)
- Microsoft.EntityFrameworkCore.InMemory (>= 8.0.8)
- Microsoft.EntityFrameworkCore.Sqlite (>= 8.0.8)
- Microsoft.EntityFrameworkCore.SqlServer (>= 8.0.8)
- MySql.EntityFrameworkCore (>= 8.0.5)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 8.0.8)
- System.Linq.Dynamic.Core (>= 1.4.5)
- System.Net.Http (>= 4.3.4)
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.14 | 82 | 10/13/2024 |
1.0.13 | 80 | 10/7/2024 |
1.0.12 | 88 | 10/6/2024 |
1.0.11 | 83 | 10/5/2024 |
1.0.10 | 90 | 10/3/2024 |
1.0.9 | 589 | 10/26/2023 |
1.0.8 | 490 | 10/26/2023 |
1.0.6 | 896 | 12/20/2021 |
1.0.5 | 1,079 | 10/12/2020 |
1.0.4 | 1,061 | 8/20/2020 |
1.0.3 | 1,093 | 8/6/2020 |
1.0.2 | 1,116 | 8/2/2020 |
1.0.1 | 1,054 | 8/1/2020 |
1.0.0-alpha-2 | 893 | 7/31/2020 |
1.0.0-alpha | 918 | 7/30/2020 |