RuoVea.DynamicWebApi
2.0.0.1
See the version list below for details.
dotnet add package RuoVea.DynamicWebApi --version 2.0.0.1
NuGet\Install-Package RuoVea.DynamicWebApi -Version 2.0.0.1
<PackageReference Include="RuoVea.DynamicWebApi" Version="2.0.0.1" />
<PackageVersion Include="RuoVea.DynamicWebApi" Version="2.0.0.1" />
<PackageReference Include="RuoVea.DynamicWebApi" />
paket add RuoVea.DynamicWebApi --version 2.0.0.1
#r "nuget: RuoVea.DynamicWebApi, 2.0.0.1"
#:package RuoVea.DynamicWebApi@2.0.0.1
#addin nuget:?package=RuoVea.DynamicWebApi&version=2.0.0.1
#tool nuget:?package=RuoVea.DynamicWebApi&version=2.0.0.1
RuoVea.DynamicWebApi
零 Controller 文件,自动生成 RESTful WebApi — 受 ABP 框架启发,将服务类动态暴露为 WebApi 端点。根据方法命名约定自动分配 HTTP 动词与路由,无需手动编写 Controller 层。
📖 目录
📋 概览
RuoVea.DynamicWebApi 在应用启动时自动将标记了 [DynamicWebApi] 的服务类转换为 ASP.NET Core Controller,根据方法名前缀自动分配 HTTP 动词。开发者只需关注 Service 业务逻辑,无需手动编写 Controller 文件。
┌───────────────────────────────────────────────────────┐
│ RuoVea.DynamicWebApi │
├───────────────────────────────────────────────────────┤
│ 标记接口 特性控制 │
│ ├─ IDynamicWebApi ├─ [DynamicWebApi] │
│ └─ IApplicationService ├─ [NonDynamicWebApi] │
│ └─ [NonDynamicMethod] │
│ │
│ HTTP 动词映射 路由生成 │
│ ├─ add/create/post→POST ├─ {prefix}/{area}/{ctrl} │
│ ├─ get/find/fetch→GET │ /{action} │
│ ├─ update/put→PUT └─ 自动去前缀/去后缀 │
│ └─ delete/remove→DELETE │
│ │
│ 扩展点 │
│ ├─ ISelectController (自定义控制器筛选) │
│ └─ IActionRouteFactory (自定义路由生成) │
└───────────────────────────────────────────────────────┘
设计原则
| 原则 | 说明 |
|---|---|
| 约定优于配置 | 方法命名决定 HTTP 动词(GetUsers → GET /api/users),无需显式声明 |
| 启动时动态生成 | 通过 ApplicationPartManager 与 IApplicationModelConvention 在启动阶段注入路由 |
| 零文件生成 | 不产生 .cs 文件或编译中间产物,纯运行时动态绑定 |
| 可扩展 | ISelectController 与 IActionRouteFactory 支持完全替换默认逻辑 |
| 零额外依赖 | 仅依赖 Microsoft.AspNetCore.App 框架引用 |
目标框架支持
| TFM | 包版本线 | 适用场景 |
|---|---|---|
netstandard2.0 |
2.0.x | .NET Framework 4.6.2+ / .NET Core 3.0+ / .NET 5–10 |
netstandard2.1 |
2.1.x | 仅 RuoVea.ExSugar:.NET Core 3.0+ / .NET 5–10(官方 ns2.1 版 ORM) |
net8.0 |
8.0.x | .NET 8/9(原生 ASP.NET Core 8 资产) |
net10.0 |
10.0.x | .NET 10(原生 ASP.NET Core 10 资产) |
📦 安装
.NET CLI
# .NET Framework 4.6.2+ / .NET Core 3.0+ / .NET 5-10(netstandard2.0)
dotnet add package RuoVea.DynamicWebApi --version 2.0.0.1
# .NET 8.0
dotnet add package RuoVea.DynamicWebApi --version 8.0.0.0
# .NET 10.0
dotnet add package RuoVea.DynamicWebApi --version 10.0.0.0
Package Manager
Install-Package RuoVea.DynamicWebApi -Version 8.0.0.0
PackageReference
<PackageReference Include="RuoVea.DynamicWebApi" Version="8.0.0.0" />
依赖项
| 依赖 | 类型 | 说明 |
|---|---|---|
Microsoft.AspNetCore.App |
框架引用 (FrameworkReference) | 无需额外 NuGet 包 |
支持的 Target Framework
| TFM | 最低版本 |
|---|---|
net8.0 |
8.0.0.0 |
net10.0 |
10.0.0.0 |
⚡ 30 秒快速开始
1. 定义服务类
using RuoVea.DynamicWebApi;
using RuoVea.DynamicWebApi.Attributes;
// 标记 [DynamicWebApi] 并实现 IDynamicWebApi,即可自动生成 API
[DynamicWebApi]
public class AppleAppService : IDynamicWebApi
{
private static readonly Dictionary<int, string> Apples = new()
{
[1] = "Big Apple",
[2] = "Small Apple"
};
/// <summary>获取单个 Apple</summary>
[HttpGet("{id:int}")]
public string Get(int id) => Apples.TryGetValue(id, out var v) ? v : "No Apple!";
/// <summary>获取所有 Apple</summary>
public IEnumerable<string> GetAll() => Apples.Values;
/// <summary>获取所有 Apple(异步)</summary>
public async Task<IEnumerable<string>> GetAllAsync() => await Task.FromResult(Apples.Values);
/// <summary>创建 Apple</summary>
public AppleDto Create(CreateAppleDto dto) => new() { Id = 3, Name = dto.Name };
/// <summary>更新 Apple</summary>
public void Update(UpdateAppleDto dto) { /* ... */ }
/// <summary>删除 Apple</summary>
[HttpDelete("{id:int}")]
public void Delete(int id) { /* ... */ }
}
2. 注册服务
// Program.cs / Startup.cs
var builder = WebApplication.CreateBuilder(args);
// ⚠️ 必须先调用 AddControllers(),再调用 AddDynamicWebApi()
builder.Services.AddControllers();
builder.Services.AddDynamicWebApi(); // 使用默认配置
// 或自定义配置
builder.Services.AddDynamicWebApi(options =>
{
options.DefaultApiPrefix = "api/v2"; // URL 前缀 → /api/v2/Apple/Get
options.DefaultHttpVerb = "POST"; // 默认 HTTP 动词
options.RemoveActionPostfixes.Clear(); // 保留 "Async" 后缀
});
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.MapControllers();
app.Run();
3. 运行验证
启动后访问 Swagger,自动生成的端点:
| 方法 | HTTP 动词 | 路由 | 说明 |
|---|---|---|---|
Get(int id) |
GET | /api/Apple/{id} |
[HttpGet] 显式指定 |
GetAll() |
GET | /api/Apple/All |
Get 前缀 → GET |
GetAllAsync() |
GET | /api/Apple/All |
Async 后缀自动去除 |
Create(CreateAppleDto) |
POST | /api/Apple |
Create 前缀 → POST,前缀去除 |
Update(UpdateAppleDto) |
PUT | /api/Apple |
Update 前缀 → PUT,前缀去除 |
Delete(int id) |
DELETE | /api/Apple/{id} |
[HttpDelete] 显式指定 |
30 秒内你完成了: 定义服务类 → 标记特性 → 注册组件 → 自动获得完整 RESTful API。
🧩 核心场景
场景一:DDD 应用服务层一键暴露
┌─────────────────────────────────────────────┐
│ 客户端 / 前端 │
└──────────────┬──────────────────────────────┘
│ HTTP
▼
┌─────────────────────────────────────────────┐
│ ASP.NET Core WebApi Host │
│ ┌───────────────────────────────────────┐ │
│ │ Swagger / Swashbuckle │ │
│ └───────────────────────────────────────┘ │
│ ┌───────────────────────────────────────┐ │
│ │ RuoVea.DynamicWebApi(动态生成) │ │
│ │ │ │
│ │ UserAppService → /api/User │ │
│ │ OrderAppService → /api/Order │ │
│ │ ProductAppService → /api/Product │ │
│ └───────────────────────────────────────┘ │
│ ┌───────────────────────────────────────┐ │
│ │ 业务逻辑层(Domain) │ │
│ └───────────────────────────────────────┘ │
└─────────────────────────────────────────────┘
// 定义接口,继承 IApplicationService 自动获得 [DynamicWebApi]
public interface IUserAppService : IApplicationService
{
Task<UserDto> GetAsync(int id);
Task<List<UserDto>> GetAllAsync();
Task<UserDto> CreateAsync(CreateUserDto input);
Task<UserDto> UpdateAsync(UpdateUserDto input);
Task DeleteAsync(int id);
}
// 实现类只关注业务逻辑
public class UserAppService : IUserAppService
{
private readonly IUserRepository _repository;
public UserAppService(IUserRepository repository) => _repository = repository;
public async Task<UserDto> GetAsync(int id) => await _repository.FindAsync(id);
public async Task<List<UserDto>> GetAllAsync() => await _repository.ToListAsync();
public async Task<UserDto> CreateAsync(CreateUserDto input) => await _repository.InsertAsync(input);
public async Task<UserDto> UpdateAsync(UpdateUserDto input) => await _repository.UpdateAsync(input);
public async Task DeleteAsync(int id) => await _repository.DeleteAsync(id);
}
❗ 性能说明:
DynamicWebApiConvention.Apply()仅在应用启动时执行一次,运行时无额外反射开销。路由匹配与模型绑定均走 ASP.NET Core 原生管道,性能与手动编写 Controller 完全一致。
场景二:按模块分区(Area 路由)
// 通过 Module 属性指定 Area
[DynamicWebApi(Module = "Admin")]
public class AdminUserService : IDynamicWebApi
{
public async Task<UserDto> GetAsync(int id) { /* ... */ }
// 路由: /api/Admin/AdminUser/{id}
}
[DynamicWebApi(Module = "Public")]
public class PublicArticleService : IDynamicWebApi
{
public async Task<List<ArticleDto>> QueryAsync() { /* ... */ }
// 路由: /api/Public/PublicArticle
}
场景三:非侵入式 —— 使用 IApplicationService
IApplicationService 已预置 [DynamicWebApi],实现该接口即可自动生成 API,无需额外标注:
// IApplicationService 已内置 [DynamicWebApi]
public class OrderAppService : IApplicationService
{
public Task<OrderDto> GetAsync(int id) { /* ... */ }
public Task<OrderDto> CreateAsync(CreateOrderDto input) { /* ... */ }
// 自动暴露为 GET /api/Order/{id} 和 POST /api/Order
}
场景四:排除特定类与方法
[DynamicWebApi]
public class MixedService : IDynamicWebApi
{
/// <summary>正常暴露</summary>
public string GetInfo() => "public info"; // ✅ 暴露为 API
/// <summary>隐藏 Swagger 但保留路由</summary>
[NonDynamicMethod]
public string InternalHelper() => "secret"; // ⚠️ 路由可访问,Swagger 不可见
/// <summary>完全排除</summary>
[NonDynamicWebApi]
public void DangerousOperation() { } // ❌ 完全不生成路由
}
| 特性 | 作用范围 | 效果 |
|---|---|---|
[NonDynamicMethod] |
Method | 路由保留,ApiExplorer.IsVisible = false,Swagger 不可见 |
[NonDynamicWebApi] |
Class / Interface / Method | 完全排除,不生成任何路由 |
场景五:按程序集配置独立路由
services.AddDynamicWebApi(options =>
{
// 当前程序集 → /apis/User/Get
options.AddAssemblyOptions(this.GetType().Assembly, apiPreFix: "apis");
// 外部程序集 → /external/Other/Get,所有方法默认 POST
options.AddAssemblyOptions(
typeof(OtherService).Assembly,
apiPreFix: "external",
httpVerb: "POST"
);
});
路由前缀优先级:程序集级 ApiPrefix > 全局 DefaultApiPrefix。
场景六:自定义扩展点(ISelectController + IActionRouteFactory)
当默认的 IDynamicWebApi + [DynamicWebApi] 双重条件不满足需求时:
// 自定义控制器筛选:基于自定义 ServiceAttribute
[AttributeUsage(AttributeTargets.Class)]
public class ServiceAttribute : Attribute
{
public string ServiceName { get; set; }
}
internal class CustomSelectController : ISelectController
{
public bool IsController(Type type)
=> type.IsPublic && type.GetCustomAttribute<ServiceAttribute>() != null;
}
// 自定义路由工厂
internal class CustomRouteFactory : IActionRouteFactory
{
public string CreateActionRouteModel(string areaName, string controllerName, ActionModel action)
{
var serviceAttr = action.ActionMethod.DeclaringType!
.GetCustomAttribute<ServiceAttribute>();
var name = string.IsNullOrEmpty(serviceAttr?.ServiceName)
? controllerName.Replace("Service", "")
: serviceAttr.ServiceName.Replace("Service", "");
var actionName = action.ActionName.Replace("Async", "");
return $"api/{name}/{actionName}";
}
}
// 注册核心 DI 服务
services.AddDynamicWebApiCore<CustomSelectController, CustomRouteFactory>();
❗ 关键限制:
AddDynamicWebApiCore<TSelect, TRouteFactory>仅注册 DI 服务,不复制配置到AppConsts。你仍需调用AddDynamicWebApi()来完成AppConsts与MvcOptions配置写入,并在 options 中设置SelectController和ActionRouteFactory覆盖默认实例。
场景七:显式路由覆盖
自动推断的路由可被 ASP.NET Core 内置路由特性覆盖:
[DynamicWebApi]
public class ProductAppService : IDynamicWebApi
{
[HttpGet("products/{id:int}")] // 覆盖自动路由
[ProducesResponseType<ProductDto>(200)] // Swagger 元数据
public async Task<ProductDto> GetAsync(int id) { /*...*/ }
[HttpPost("products")]
[ProducesResponseType<ProductDto>(201)]
public async Task<ProductDto> CreateAsync([FromBody] CreateProductDto input) { /*...*/ }
}
❗ 当
Selector已包含AttributeRouteModel时,DynamicWebApiConvention会跳过自动路由配置,此时完全由你手动控制路由。
场景八:外部程序集 + UseDynamicWebApi 两步注册
适用于需要在 Configure 阶段动态添加外部程序集:
// Step 1: ConfigureServices — 注册核心服务
services.AddDynamicWebApiCore<CustomSelectController, CustomRouteFactory>();
// Step 2: Configure — 使用 UseDynamicWebApi 添加外部程序集
app.UseDynamicWebApi((serviceProvider, options) =>
{
options.AddAssemblyOptions(typeof(ExternalService).Assembly, apiPreFix: "ext");
});
❗ 重复调用警告:
AddDynamicWebApi()和UseDynamicWebApi()都会向AppConsts写入配置。同时调用两者会导致配置被覆盖两次。推荐只使用AddDynamicWebApi()配置,UseDynamicWebApi()仅在非标准宿主场景下使用。
⚙️ 配置选项详解
DynamicWebApiOptions 完整参数
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
DefaultHttpVerb |
string |
"POST" |
无法匹配动词映射表时的默认 HTTP 动词 |
DefaultAreaName |
string |
"" |
全局 Area 路由名称。可为 null,Valid() 后转为 "" |
DefaultApiPrefix |
string |
"api" |
全局路由前缀,自动添加到所有 API |
RemovePrefix |
List<string> |
"add","create","post","get","find","fetch","query","update","put","delete","remove" |
从 ActionName 中去除的方法名前缀列表(小写匹配) |
RemoveControllerPostfixes |
List<string> |
"Service","AppService","ApplicationService" |
从 ControllerName 中去除的类名后缀列表 |
RemoveActionPostfixes |
List<string> |
"Async" |
从 ActionName 中去除的方法名后缀列表 |
FormBodyBindingIgnoredTypes |
List<Type> |
typeof(IFormFile) |
不自动添加 [FromBody] 绑定的参数类型 |
GetRestFulActionName |
Func<string, string> |
null |
自定义 ActionName 处理委托。返回 null 则退回到默认处理逻辑 |
SelectController |
ISelectController |
DefaultSelectController |
自定义控制器选择策略实例 |
ActionRouteFactory |
IActionRouteFactory |
DefaultActionRouteFactory |
自定义路由生成策略实例 |
AssemblyDynamicWebApiOptions |
Dictionary<Assembly, AssemblyDynamicWebApiOptions> |
new() (空字典) |
按程序集指定独立的路由前缀和 HTTP 动词 |
AssemblyDynamicWebApiOptions
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
ApiPrefix |
string (只读) |
null |
覆盖该程序集的路由前缀。null 表示使用全局 DefaultApiPrefix |
HttpVerb |
string (只读) |
null |
覆盖该程序集的 HTTP 动词。null 表示使用动词映射 |
HTTP 动词映射表
方法名前缀通过 GetPascalOrCamelCaseFirstWord() 提取首单词(不区分大小写),与下表匹配:
| 方法名前缀(小写) | HTTP 动词 | 前缀是否去除 |
|---|---|---|
add, create, post |
POST | ✅ |
get, find, fetch, query |
GET | ✅ |
update, put |
PUT | ✅ |
delete, remove |
DELETE | ✅ |
| 其他任意前缀 | DefaultHttpVerb(默认 POST) |
❌ |
类名 → Controller 名规则
| 服务类名 | 匹配后缀 | Controller 名 |
|---|---|---|
UserService |
Service |
User |
OrderAppService |
AppService |
Order |
ProductApplicationService |
ApplicationService |
Product |
PaymentManager |
(无一匹配) | PaymentManager |
三种注册方式
// 方式 1:默认配置(最简单)
services.AddDynamicWebApi();
// 方式 2:委托配置(推荐)
services.AddDynamicWebApi(options =>
{
options.DefaultApiPrefix = "apis";
options.DefaultAreaName = "v1";
options.RemoveActionPostfixes.Clear();
options.GetRestFulActionName = actionName => actionName;
options.AddAssemblyOptions(
typeof(ExternalAppService).Assembly,
apiPreFix: "external",
httpVerb: "POST"
);
});
// 方式 3:预构建 options 对象
var webApiOptions = new DynamicWebApiOptions
{
DefaultHttpVerb = "POST",
DefaultApiPrefix = "services"
};
services.AddDynamicWebApi(webApiOptions);
工作原理流程
应用程序启动
│
├─► AddDynamicWebApi()
│ │
│ ├─► 验证 options.Valid()
│ ├─► 复制配置到 AppConsts(全局静态配置)
│ ├─► 注册 DynamicWebApiControllerFeatureProvider
│ └─► 注册 DynamicWebApiConvention 到 MvcOptions.Conventions
│
▼
MVC 构建 ApplicationModel
│
├─► ControllerFeatureProvider.IsController(type)
│ │
│ └─► DefaultSelectController.IsController(type)
│ ├─ type 实现 IDynamicWebApi? ──否──► ❌ 不是 Controller
│ ├─ type 是 public + 非抽象 + 非泛型? ──否──► ❌ 不是 Controller
│ ├─ type 或接口上有 [DynamicWebApi]? ──否──► ❌ 不是 Controller
│ └─ type 上有 [NonDynamicWebApi]? ──是──► ❌ 不是 Controller
│ │
│ 否
│ ▼
│ ✅ 是 Controller
│
└─► DynamicWebApiConvention.Apply(applicationModel)
├─► ConfigureArea —— 设置 area 路由值
├─► ConfigureApiExplorer —— Swagger 可见性
├─► ConfigureSelector —— 路由 + HTTP 动词
│ ├─► 方法名前缀 → HTTP 动词映射
│ ├─► 去除前缀 + 去除后缀 → 最终 ActionName
│ └─► 生成路由: {prefix}/{area}/{controller}/{action}
├─► ConfigureParameters —— 复杂类型自动 [FromBody]
└─► CheckNoMapMethod —— [NonDynamicMethod] 方法隐藏
🛡️ 错误处理
启动时配置校验
DynamicWebApiOptions.Valid() 在注册时自动调用,不符合条件则抛出异常:
| 条件 | 异常类型 | 消息 |
|---|---|---|
DefaultHttpVerb 为 null 或空 |
ArgumentException |
DefaultHttpVerb can not be empty. |
FormBodyBindingIgnoredTypes 为 null |
ArgumentException |
FormBodyBindingIgnoredTypes can not be null. |
RemoveControllerPostfixes 为 null |
ArgumentException |
RemoveControllerPostfixes can not be null. |
AddMvc() / AddControllers() 未先调用 |
InvalidOperationException |
"AddDynamicWebApi" must be after "AddMvc". |
options 参数为 null |
ArgumentException |
(参数名为 nameof(options)) |
assembly 参数为 null(AddAssemblyOptions) |
ArgumentException |
assembly can not be null. |
DefaultAreaName 和 DefaultApiPrefix 为 null 时不抛异常,而是自动设为 string.Empty。
调用顺序强制
// ❌ 错误:在 AddControllers 之前调用
builder.Services.AddDynamicWebApi();
builder.Services.AddControllers();
// → InvalidOperationException: "AddDynamicWebApi" must be after "AddMvc".
// ✅ 正确
builder.Services.AddControllers();
builder.Services.AddDynamicWebApi();
常见问题排查
| 问题 | 可能原因 | 排查方向 |
|---|---|---|
| 路由 404 | 服务类未被识别为 Controller | 检查是否实现 IDynamicWebApi、是否标记 [DynamicWebApi]、是否为 public 非抽象非泛型类 |
| 所有路由都是 POST | 方法名首单词不在动词映射表中 | 检查方法名首单词,或以 DefaultHttpVerb 覆盖 |
Controller 名仍带 Service 后缀 |
后缀不在 RemoveControllerPostfixes 列表中 |
添加自定义后缀到该列表 |
| Swagger 不显示端点 | 标记了 [NonDynamicMethod] 或 [NonDynamicWebApi] |
检查方法和类上的排除特性 |
| 泛型类/抽象类不生成 | DefaultSelectController 自动排除 |
实现 ISelectController 自定义筛选逻辑 |
🧵 线程安全
| 组件 | 线程安全 | 说明 |
|---|---|---|
DynamicWebApiOptions |
⚠️ 否 | 配置对象应在启动时构造并注册,运行时不修改 |
AppConsts |
⚠️ 否 | 全局静态状态,设计约定:启动时写入一次,运行时应为只读 |
DefaultSelectController |
✅ 是 | 纯计算,无共享可变状态 |
DefaultActionRouteFactory |
✅ 是 | 纯计算,依赖 AppConsts(假设只读) |
DynamicWebApiConvention |
✅ 是 | 由 ASP.NET Core 在启动时调用一次 |
DynamicWebApiControllerFeatureProvider |
✅ 是 | 由 ASP.NET Core 在启动时调用一次 |
DynamicWebApiServiceExtensions |
✅ 是 | 仅在 Startup / Program 中调用 |
⚠️ 重要:
AppConsts是一个全局静态配置类,所有属性均为{ get; set; }。设计约定是启动时写入一次,之后只读。运行时修改AppConsts值将导致不可预测的路由行为。
❗ 重复调用警告:
AddDynamicWebApi()和UseDynamicWebApi()的配置复制逻辑几乎逐行相同,都会向AppConsts写入。同时调用两者会导致配置覆盖。
📊 API 速查表
公开接口
| 接口 | 成员 | 说明 |
|---|---|---|
IDynamicWebApi |
(无) | 标记接口(空接口),所有动态 API 的根接口 |
IApplicationService : IDynamicWebApi |
(继承) | 预标记 [DynamicWebApi],推荐直接继承 |
ISelectController |
bool IsController(Type type) |
控制器筛选器:决定类型是否应为 Controller |
IActionRouteFactory |
string CreateActionRouteModel(string areaName, string controllerName, ActionModel action) |
路由工厂:生成路由模板字符串 |
特性
| 特性 | 作用目标 | 属性 | 说明 |
|---|---|---|---|
[DynamicWebApi] |
Interface / Class | string Module |
标记为动态 API。Module 等同于 Area 名称 |
[NonDynamicWebApi] |
Interface / Class / Method | (无) | 完全排除:不生成 Controller 或 Action(最高优先级) |
[NonDynamicMethod] |
Interface / Class / Method | (无) | 半隐藏:路由保留,Swagger 不可见 |
扩展方法
| 方法签名 | 说明 |
|---|---|
services.AddDynamicWebApi() |
使用默认选项注册 |
services.AddDynamicWebApi(Action<DynamicWebApiOptions>) |
通过委托配置 |
services.AddDynamicWebApi(DynamicWebApiOptions) |
通过实例配置 |
services.AddDynamicWebApiCore<TSelect, TRouteFactory>() |
仅注册核心 DI 服务,不配置 AppConsts |
app.UseDynamicWebApi(Action<IServiceProvider, DynamicWebApiOptions>) |
Middleware 方式配置(非标准宿主场景) |
路由模板
{ApiPrefix}/{AreaName}/{ControllerName}/{ActionName}
ApiPrefix:程序集配置优先 → 全局DefaultApiPrefixAreaName:[DynamicWebApi(Module = "...")]优先 → 全局DefaultAreaNameControllerName:类名去除RemoveControllerPostfixesActionName:方法名去除RemoveActionPostfixes,再去除RemovePrefix,最后经GetRestFulActionName处理
DynamicWebApiAttribute 内部静态方法 ⚠️
以下 6 个 internal static 方法全部检查同一属性类型(疑似移植时遗留的 bug):
// 全部检查 DynamicWebApiAttribute —— 显然不正确
internal static bool IsExplicitlyEnabledFor(Type type) // → 检查 DynamicWebApiAttribute
internal static bool IsExplicitlyDisabledFor(Type type) // → 检查 DynamicWebApiAttribute ❗应为 NonDynamicWebApiAttribute
internal static bool IsMetadataExplicitlyEnabledFor(Type type) // → 检查 DynamicWebApiAttribute
internal static bool IsMetadataExplicitlyDisabledFor(Type type) // → 检查 DynamicWebApiAttribute ❗应为 NonDynamicWebApiAttribute
internal static bool IsMetadataExplicitlyDisabledFor(MethodInfo) // → 检查 DynamicWebApiAttribute ❗应为 NonDynamicWebApiAttribute
internal static bool IsMetadataExplicitlyEnabledFor(MethodInfo) // → 检查 DynamicWebApiAttribute
❗ 这些方法在当前版本中未被框架自身调用(特性检测走
ReflectionHelper.GetSingleAttributeOrDefaultByFullSearch路径),但未来若启用请先行修复。
🗺️ 版本迁移指南
从手动 Controller 迁移
| 旧代码模式 | 迁移到 RuoVea.DynamicWebApi |
|---|---|
手动 [ApiController] + [Route] + [HttpPost] 类 |
接口继承 IApplicationService,方法按约定命名 |
ControllerBase 子类 |
无需 Controller 类,服务类直接实现接口 |
Swagger 手动 [ApiExplorerSettings(IgnoreApi = true)] |
使用 [NonDynamicMethod] 标记 |
从 ABP / Panda.DynamicWebApi 迁移
| ABP 概念 | RuoVea.DynamicWebApi 对应 |
|---|---|
IRemoteService |
IDynamicWebApi |
IApplicationService |
IApplicationService(同名、同预标记行为) |
[RemoteService] |
[DynamicWebApi] |
RemoteServiceAttribute.IsEnabled 设为 false |
[NonDynamicWebApi] |
| ABP 模块系统注入 | 独立 AddDynamicWebApi() + 程序集级配置 |
AbpServiceConvention |
DynamicWebApiConvention |
v8.0.x → v10.0.x
- API 无破坏性变更。v10.0.x 仅在
net10.0TFM 上编译,所有公开 API 签名保持一致。 - 唯一依赖为
Microsoft.AspNetCore.App框架引用,无第三方 NuGet 包。
📄 License
MIT License © RuoVea
🔗 相关资源: NuGet Gallery · 问题反馈
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
NuGet packages (14)
Showing the top 5 NuGet packages that depend on RuoVea.DynamicWebApi:
| Package | Downloads |
|---|---|
|
RuoVea.OmiApi.Config
系统配置管理 |
|
|
RuoVea.OmiApi.SystemApp
系统应用管理 |
|
|
RuoVea.OmiApi.UserRoleMenu
用户角色菜单管理 |
|
|
RuoVea.OmiApi.UserRole
用户角色管理 |
|
|
RuoVea.OmiApi.Auth
认证授权模块 |
GitHub repositories
This package is not used by any popular GitHub repositories.