Galosys.Foundation.AspNetCore.DynamicApi 26.5.15.1

dotnet add package Galosys.Foundation.AspNetCore.DynamicApi --version 26.5.15.1
                    
NuGet\Install-Package Galosys.Foundation.AspNetCore.DynamicApi -Version 26.5.15.1
                    
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="Galosys.Foundation.AspNetCore.DynamicApi" Version="26.5.15.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.AspNetCore.DynamicApi" Version="26.5.15.1" />
                    
Directory.Packages.props
<PackageReference Include="Galosys.Foundation.AspNetCore.DynamicApi" />
                    
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 Galosys.Foundation.AspNetCore.DynamicApi --version 26.5.15.1
                    
#r "nuget: Galosys.Foundation.AspNetCore.DynamicApi, 26.5.15.1"
                    
#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 Galosys.Foundation.AspNetCore.DynamicApi@26.5.15.1
                    
#: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=Galosys.Foundation.AspNetCore.DynamicApi&version=26.5.15.1
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.AspNetCore.DynamicApi&version=26.5.15.1
                    
Install as a Cake Tool

Galosys.Foundation.AspNetCore.DynamicApi

成熟度: 🟢 稳定 — 生产可用,测试充分,活跃维护

简介

Galosys.Foundation.AspNetCore.DynamicApi 是 ASP.NET Core 动态 API 生成模块,通过特性标记自动将服务类转换为 RESTful API 控制器,支持自定义路由规则和统一响应模型。

特性

动态 API 生成

  • 自动控制器发现 - 基于 [RestController] 特性自动识别服务类
  • 约定式路由 - 根据命名空间和类名自动生成 RESTful 路由
  • 方法名转换 - 自动移除 Async 等后缀生成简洁的 Action 名称
  • 路由命名策略 - 支持 CamelCase、SnakeCase、KebabCase 命名规则

统一响应模型

  • 成功响应 - Succeed(data) 返回标准成功响应
  • 失败响应 - Fail(errorCode, errorMsg) 返回标准错误响应
  • 类型安全 - 支持泛型响应模型

路由生成规则

/api/{version}/{namespace}/{controller}/{action}
  • 版本 - 默认 v1
  • 命名空间 - 移除 ApplicationApp 等后缀
  • 控制器 - 移除 AppServiceService 等后缀
  • 动作 - 移除 Async 后缀

安装

dotnet add package Galosys.Foundation.AspNetCore.DynamicApi

使用

注册服务

services.AddDynamicApi(); // 注册动态 API 服务

定义服务类

[RestController]
public class OrderAppService
{
    public async Task<OrderDto> GetOrderAsync(int id)
    {
        // 业务逻辑
    }
    
    public async Task<List<OrderDto>> GetOrdersAsync()
    {
        // 业务逻辑
    }
    
    public async Task<OrderDto> CreateOrderAsync(CreateOrderRequest request)
    {
        // 业务逻辑
    }
}

使用统一响应

[RestController]
public class ProductAppService : RestController
{
    public UnifiedResponse GetProduct(int id)
    {
        var product = _repository.Get(id);
        if (product == null)
            return Fail(BizErrorCode.NotFound, "产品不存在");
        
        return Succeed(product);
    }
}

配置路由策略

[RestController(CasingStrategy = CasingStrategy.KebabCase)]
public class UserAppService
{
    // 路由: /api/v1/user/get-user
    public async Task<UserDto> GetUserAsync(int id)
    {
        // 业务逻辑
    }
}

自定义版本和场景

[RestController(Scene = "admin", Version = "v2")]
public class AdminOrderAppService
{
    // 路由: /admin/v2/admin-order/get-order
    public async Task<OrderDto> GetOrderAsync(int id)
    {
        // 业务逻辑
    }
}

核心类

说明
RestControllerAttribute 标记服务类为动态 API 控制器
RestController 控制器基类,提供统一响应方法
RestControllerConvertion 路由约定处理器,自动生成路由
RestControllerFeatureProvider 控制器特性发现提供程序

路由命名策略

策略 示例路由 说明
CamelCase /api/v1/orderApp/getOrder 驼峰命名
SnakeCase /api/v1/order_app/get_order 下划线命名
KebabCase /api/v1/order-app/get-order 短横线命名

配置选项

[RestController(
    Scene = "api",           // 路由场景前缀
    Version = "v1",          // API 版本
    CasingStrategy = CasingStrategy.KebabCase  // 命名策略
)]

支持的后缀约定

控制器后缀(自动移除):

  • AppService, AppSrv, AppSvc
  • ApplicationService, ApplicationSrv, ApplicationSvc
  • Service, Svc

方法后缀(自动移除):

  • Async

命名空间后缀(自动移除):

  • Application, App

依赖

  • Microsoft.AspNetCore.App
  • Galosys.Foundation.AspNetCore
  • Galosys.Foundation.Core
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.  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 (9)

Showing the top 5 NuGet packages that depend on Galosys.Foundation.AspNetCore.DynamicApi:

Package Downloads
Galosys.Foundation.AspNetCore.Quartz

Galosys.Foundation快速开发库

Galosys.Foundation.AspNetCore.LazySlideCaptcha

Galosys.Foundation快速开发库

Galosys.Foundation.AspNetCore.Aliyun

Galosys.Foundation快速开发库

Galosys.Foundation.AspNetCore.LazyCaptcha

Galosys.Foundation快速开发库

Galosys.AspNetCore.Application

Galosys.Foundation快速开发库

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.5.15.1 60 5/15/2026
26.5.12.3 205 5/12/2026
26.5.12.2 220 5/12/2026
26.4.27.1-rc1 205 4/26/2026
26.4.25.1-rc1 221 4/25/2026
26.4.22.2-rc7 221 4/22/2026
26.4.22.2-rc6 215 4/22/2026
26.4.22.2-rc4 217 4/22/2026
26.4.22.2-rc3 202 4/22/2026
26.4.19.1-rc1 194 4/19/2026
26.1.30.1-rc1 322 1/30/2026
26.1.29.1 326 1/29/2026
26.1.28.5 344 1/28/2026
26.1.28.4 326 1/28/2026
26.1.28.2 329 1/28/2026
26.1.23.6 315 1/23/2026
26.1.21.1 289 1/21/2026
26.1.2.1 301 1/2/2026
26.1.1.1 300 1/1/2026
25.12.12.4 350 12/12/2025
Loading failed