Galosys.Foundation.AspNetCore.DynamicApi 26.7.19.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Galosys.Foundation.AspNetCore.DynamicApi --version 26.7.19.1
                    
NuGet\Install-Package Galosys.Foundation.AspNetCore.DynamicApi -Version 26.7.19.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.7.19.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.7.19.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.7.19.1
                    
#r "nuget: Galosys.Foundation.AspNetCore.DynamicApi, 26.7.19.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.7.19.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.7.19.1
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.AspNetCore.DynamicApi&version=26.7.19.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
  • 命名空间 - 移除 Application、App 等后缀
  • 控制器 - 移除 AppService、Service 等后缀
  • 动作 - 移除 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 net10.0 is compatible.  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.LazySlideCaptcha

Galosys.Foundation快速开发库

Galosys.Foundation.AspNetCore.Aliyun

Galosys.Foundation快速开发库

Galosys.Foundation.AspNetCore.LazyCaptcha

Galosys.Foundation快速开发库

Galosys.Foundation.AspNetCore.Localization

Galosys.Foundation快速开发库

Galosys.Foundation.MiniProfiler

Galosys.Foundation快速开发库

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.10.9.1 51 10/9/2026
26.10.8.1 77 10/8/2026
26.9.23.1 143 9/23/2026
26.9.16.1 142 9/16/2026
26.9.15.1 140 9/15/2026
26.9.14.1 135 9/14/2026
26.9.10.1 163 9/10/2026
26.9.3.1 146 9/3/2026
26.8.29.1 153 8/31/2026
26.8.26.1 151 8/26/2026
26.8.23.1 185 8/23/2026
26.8.21.1 154 8/21/2026
26.8.20.1 162 8/20/2026
26.8.18.1 169 8/18/2026
26.8.17.1 167 8/17/2026
26.8.13.2 161 8/13/2026
26.8.13.1 150 8/13/2026
26.8.12.2 136 8/12/2026
26.8.12.1 150 8/12/2026
26.7.19.1 185 7/19/2026
Loading failed