ClieanLite.DynamicApi
1.1.1
dotnet add package ClieanLite.DynamicApi --version 1.1.1
NuGet\Install-Package ClieanLite.DynamicApi -Version 1.1.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="ClieanLite.DynamicApi" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ClieanLite.DynamicApi" Version="1.1.1" />
<PackageReference Include="ClieanLite.DynamicApi" />
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.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ClieanLite.DynamicApi, 1.1.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 ClieanLite.DynamicApi@1.1.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=ClieanLite.DynamicApi&version=1.1.1
#tool nuget:?package=ClieanLite.DynamicApi&version=1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ClieanLite.DynamicApi 是一个轻量级且强大的 .NET 库,旨在帮助开发者快速、动态地构建和暴露 API 接口,极大地简化了 API 定义和路由配置的复杂性。它支持在应用程序运行时根据约定或配置自动生成 API 端点,并提供灵活的配置选项,让你的应用更具扩展性和适应性。
✨特点 (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:
...
// 配置 Swagger。注意,如果使用 DynamicApi,你需要手动配置 Swagger 的 XML 注释。
// 例如:
//<GenerateDocumentationFile>True</GenerateDocumentationFile>
//<DocumentationFile>CleanLite.xml</DocumentationFile>
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo() { Title = "Dynamic WebApi", Version = "v1" });
options.DocInclusionPredicate((docName, description) => true);
var basePath = AppDomain.CurrentDomain.BaseDirectory;
var xmlPath = Path.Combine(basePath, "CleanLite.xml");
options.IncludeXmlComments(xmlPath);
});
builder.Services.AddControllers();
builder.Services.AddDynamicApi(options =>
{
// 默认的 DefaultCustomControllerFeatureProvider 总是返回 true,
// 这意味着 DynamicApi Convention 会尝试处理所有被识别为“控制器”的类型。
// 如果你需要更精细的控制,可以自定义实现 ICustomControllerFeatureProvider。
options.CustomControllerFeatureProvider = new DefaultCustomControllerFeatureProvider();
// 配置需要忽略的方法名后缀。例如:若方法名为 "GetUserAsync",
// 且忽略后缀包含 "Async",则最终 API 路径中方法名为 "GetUser"。
options.IgnoreMethodSuffix.AddRange([]); // 示例: new List<string> { "Async", "Handler" }
// 配置需要忽略的控制器名后缀。例如:若控制器名为 "UserService",
// 且忽略后缀包含 "Service",则最终 API 路径中控制器名为 "User"。
options.IgnoreControllerSuffix.AddRange([]); // 示例: new List<string> { "Service", "Controller" }
// 自定义所有动态 API 的路由前缀。默认为 "api"。
// 最终路由将形如 /{ApiPrefix}/{ControllerName}/{ActionName}
options.ApiPrefix = "api";
// 配置要扫描的程序集。
// DynamicApi 将扫描这些程序集以查找实现 IDynamicApi 接口的类作为动态 API 源。
// 如果你的动态 API 类通过 IoC 容器(如 Autofac, DryIoc)进行注入,
// 且这些类不在主程序集,你需要在这里明确指定它们所在的程序集。
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 | Versions 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.
更新说明文档