MicroApi.Core 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MicroApi.Core --version 1.2.0
                    
NuGet\Install-Package MicroApi.Core -Version 1.2.0
                    
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="MicroApi.Core" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MicroApi.Core" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="MicroApi.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MicroApi.Core --version 1.2.0
                    
#r "nuget: MicroApi.Core, 1.2.0"
                    
#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.
#:package MicroApi.Core@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MicroApi.Core&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=MicroApi.Core&version=1.2.0
                    
Install as a Cake Tool

English

MicroApi

基于 SqlKata Query Builder 的表驱动 Restful API 中间件。

v1.2:支持资源映射(路由别名、列别名、/{id})。未声明资源默认不可访问;PUT/DELETE 必须带过滤条件(路径 id 也算)。

支持的数据库

  1. MySql MicroApi.MySql
  2. SqlServer MicroApi.SqlServer
  3. PostgreSQL MicroApi.PostgreSQL
  4. Oracle MicroApi.Oracle
  5. Sqlite MicroApi.Sqlite
  6. Firebird MicroApi.Firebird

接口认证中间件

MicroApi.Authorization

可选 claim:microapi.permission,格式 resource:METHOD / resource:* / *:*(resource 为 API 资源名,不是物理表名)。

示例

MicroApi.Demo

用法

简写(路径名 = 表名):

services.AddMicroApi(option =>
    {
        option.AuthorizeType = AuthorizeType.JWT;
        option.ApiRoutePrefix = "api";
        option.AllowTable("Users", ApiHttpMethods.All);
    })
    .UseSqlServer(connectionString)
    .AddMicroApiAuthorization();

资源映射(推荐:路径/JSON 与库表解耦):

services.AddMicroApi(option =>
    {
        option.MapResource("users", r =>
        {
            r.Table("app_user");
            r.Methods(ApiHttpMethods.Get | ApiHttpMethods.Put | ApiHttpMethods.Delete);
            r.Key("id", "user_id");
            r.Column("name", "user_name");
            r.Column("email", "user_email");
        });
    })
    .UseSqlServer(connectionString);
GET /api/users          → SELECT user_id, user_name, user_email FROM app_user
GET /api/users/12       → WHERE user_id = 12(未找到 404)
PUT /api/users/12       → 路径 id 即过滤条件

一旦使用 Column(...),仅映射列可读写;未映射列(如 password_hash)不会出现在查询与响应中。只声明 Key、不声明 Column 时,其它列仍按 API 名 = 数据库列名处理。

本地原型若需临时放开全部表(仍校验标识符与写保护;/{id} 默认按列 id):

option.AllowAllTables = true;

分页保护:MaxPageSize(默认 200)、DefaultPageSize(默认 10)。

中间件顺序:

app.UseAuthentication();
app.UseAuthorization();
app.UseMicroApi();

API 格式

路径:/{ApiRoutePrefix}/{resource}/{ApiRoutePrefix}/{resource}/{id}

/{id} 需要 MapResource(...).Key(...)。未配置主键时访问带 id 的路径返回 400。POST 不允许路径 id。

GET

  • 分页:?page=&size=?offset=&limit=(带 /{id} 时忽略分页)
  • 排序:?orderAsc=col1,col2 / ?orderDesc=col1(使用 API 列名)
  • 条件:?col=valuecol.ge / gt / le / lt / in / like

POST

新增,JSON body 使用 API 列名。

PUT / DELETE

按查询条件和/或路径 {id} 更新/删除;至少需要一个过滤条件,否则返回 400。

变动日志

1.2.0

  • 新增 MapResource:路由别名(usersapp_user)、列映射、主键 Key
  • 支持 GET/PUT/DELETE /{prefix}/{resource}/{id};GET 单条不存在返回 404
  • 列映射开启后,请求、过滤、排序、响应均使用 API 列名,隐藏未映射的数据库列
  • AllowTable 仍可用,行为与 1.1 兼容(不含 /{id},除非再 MapResource 并声明 Key

1.1.0

  • 默认资源白名单;PUT/DELETE 禁止无条件写删
  • 表/列标识符校验;分页上限
  • Scoped 数据库连接;异步 Handler
  • JWT 与可选 microapi.permission;CI 改为 Trusted Publishing
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on MicroApi.Core:

Package Downloads
MicroApi.MySql

MicroApi MySql provider

MicroApi.Firebird

MicroApi Firebird provider

MicroApi.Oracle

MicroApi Oracle provider

MicroApi.SqlServer

MicroApi SqlServer provider

MicroApi.Sqlite

MicroApi Sqlite provider

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.1 167 8/18/2026
1.2.0 86 8/15/2026
1.1.0 205 8/13/2026
1.0.0.3 642 11/15/2022
1.0.0.2 578 11/15/2022
1.0.0 474 11/23/2021

Resource mapping, column aliases, /{id} routes