AutoController 1.0.13

There is a newer version of this package available.
See the version list below for details.
dotnet add package AutoController --version 1.0.13                
NuGet\Install-Package AutoController -Version 1.0.13                
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="AutoController" Version="1.0.13" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AutoController --version 1.0.13                
#r "nuget: AutoController, 1.0.13"                
#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.
// Install AutoController as a Cake Addin
#addin nuget:?package=AutoController&version=1.0.13

// Install AutoController as a Cake Tool
#tool nuget:?package=AutoController&version=1.0.13                

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:

  • Pagination by default
  • Sorting results
  • Interacting type can be JSON or XML
  • Flexible and adjustable request/response schema
  • Filtering results
  • Authentificated Access and Permitions Control
  • Execute some Actions before save/delete
  • Execute some Actions before DbContext SaveChanges/SaveChangesAsync

Routers and handlers for paths will be created:

  • "/Your Entity/Index" GET
  • "/Your Entity/Count" GET
  • "/Your Entity/Save" POST
  • "/Your Entity/Update" PUT
  • "/Your Entity/Delete" DELETE

Interacting type can be JSON or XML.

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 add db context first!
            var connString = Configuration.GetConnectionString("DefaultConnection");
            services.AddDbContext<ApplicationDBContext>(options =>
                options.UseSqlite(connString));
            // then add AddAutoController service
            services.AddAutoController<ApplicationDBContext>();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // ---- Your code -----------//
            // Create your api
            // handle requests with default options
            app.UseAutoController<ApplicationDBContext>();
            // You can configure your api
            // RoutePrefix should be unique!
            var JsonOptions = new JsonSerializerOptions
            {
                WriteIndented = true
            };
            var options = new AutoControllerOptions() {
                JsonSerializerOptions = JsonOptions,
                LogInformation = true,
                RoutePrefix="api2",
                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 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. 
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.0.14 97 10/13/2024
1.0.13 88 10/7/2024
1.0.12 96 10/6/2024
1.0.11 108 10/5/2024
1.0.10 100 10/3/2024
1.0.9 593 10/26/2023
1.0.8 494 10/26/2023
1.0.6 918 12/20/2021
1.0.5 1,085 10/12/2020
1.0.4 1,067 8/20/2020
1.0.3 1,099 8/6/2020
1.0.2 1,142 8/2/2020
1.0.1 1,060 8/1/2020
1.0.0-alpha-2 895 7/31/2020
1.0.0-alpha 920 7/30/2020