ClieanLite.DynamicApi 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ClieanLite.DynamicApi --version 1.0.0
                    
NuGet\Install-Package ClieanLite.DynamicApi -Version 1.0.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="ClieanLite.DynamicApi" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ClieanLite.DynamicApi" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ClieanLite.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 ClieanLite.DynamicApi --version 1.0.0
                    
#r "nuget: ClieanLite.DynamicApi, 1.0.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 ClieanLite.DynamicApi@1.0.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=ClieanLite.DynamicApi&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ClieanLite.DynamicApi&version=1.0.0
                    
Install as a Cake Tool

✨特点 (Features)

  • 运行时 API 生成: 无需硬编码路由,根据约定或配置自动生成 API 端点,减少重复代码。
  • 轻量级设计: 设计简洁,无其他依赖,对现有项目侵入性低,保持应用程序的精简。
  • 灵活的约定与配置: 支持自定义配置路由,满足不同的业务需求。
  • 无缝集成: 轻松集成到现有的 ASP.NET Core 项目中,享受开箱即用的便利。
  • Swagger/OpenAPI 友好: 自动与 Swagger UI 良好集成,为动态生成的 API 提供完整的交互式文档。

🚀 快速开始 (Getting Started)

1. 安装 (Installation)

通过 NuGet 包管理器安装 ClieanLite.DynamicApi

dotnet add package ClieanLite.DynamicApi

或者在 Visual Studio 的 NuGet 包管理器中搜索 ClieanLite.DynamicApi 进行安装。

2. 基本配置 (Basic Configuration)

在你的 ASP.NET Core 项目的 Program.cs文件中进行配置。 Program.cs:

...
builder.Services.AddControllers();
builder.Services.AddDynamicApi(options =>
{
    //自定义控制器识别策略
    options.CustomControllerFeatureProvider = new DefaultCustomControllerFeatureProvider();
    //方法名忽略后缀
    options.IgnoreMethodSuffix.AddRange([]);
    //控制器名忽略后缀
    options.IgnoreControllerSuffix.AddRange([]);
    //自定义路由前缀
    options.ApiPrefix = "api";
    //扫描程序集(需要动态生成接口的程序集,一般为Services,若使用Autofa或其他已进行注入则此处为空即可)
    options.Assemblys.AddRange([]);
});
var app = builder.Build();
app.UseDynamicApi();
...

3. 自定义控制器识别策略 (Custom Controller Recognition Strategy)

实现 ICustomControllerFeatureProvider 接口以定义自定义控制器识别逻辑。

public class CustomControllerFeatureProvider : ICustomControllerFeatureProvider
{
    public bool IsController(Type type)
    {
        return true;
    }
}

4. 使用动态 API (Using Dynamic API)

以仓储模式进行举例

//抽象基类,包含仓储注入
public abstract class BaseService<T> : IDynamicApi where T : class
{
    [FromServices]
    public required IBaseRepository<T> Repository { get; set; }
}

//动态生成接口服务类
public class UserService : BaseService<T_Test>
{
    public async Task<List<T_Test>> GETAsync()
    {
        return await Repository.GetListAsync();
    }
}

5. 运行应用程序 (Run the Application)

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.
  • net8.0

    • No dependencies.

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.1.1 135 1/17/2026
1.0.1 203 10/12/2025
1.0.0 207 10/12/2025