BugFree.Controllers.Core
1.2.2026.623-beta1237
This is a prerelease version of BugFree.Controllers.Core.
There is a newer prerelease version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package BugFree.Controllers.Core --version 1.2.2026.623-beta1237
NuGet\Install-Package BugFree.Controllers.Core -Version 1.2.2026.623-beta1237
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="BugFree.Controllers.Core" Version="1.2.2026.623-beta1237" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BugFree.Controllers.Core" Version="1.2.2026.623-beta1237" />
<PackageReference Include="BugFree.Controllers.Core" />
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 BugFree.Controllers.Core --version 1.2.2026.623-beta1237
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: BugFree.Controllers.Core, 1.2.2026.623-beta1237"
#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 BugFree.Controllers.Core@1.2.2026.623-beta1237
#: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=BugFree.Controllers.Core&version=1.2.2026.623-beta1237&prerelease
#tool nuget:?package=BugFree.Controllers.Core&version=1.2.2026.623-beta1237&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
BugFree.Controllers.Core
BugFree 控制器核心框架,提供统一的 API 控制器基类体系、实体 CRUD 控制器、安全中间件、IP 访问控制、安全响应头注入等开箱即用的企业级 ASP.NET Core 基础设施。
特性
📦 统一控制器基类
BugFreeController— 继承ControllerBase,内置Success()/Fail()/Unauthorized()快捷响应方法- 统一
IResponseService可替换策略,支持自定义响应构建行为
🧩 实体控制器层次体系
BugFreeController
└── EmptyEntityController<TEntity> ← 实体工厂初始化
└── QueryEntityController<TEntity> ← 列表查询 (Query)
└── GetEntityController<TEntity> ← 单实体查询 (Get)
└── ReadOnlyEntityController<TEntity> ← 只读
└── EntityController<TEntity,TModel> ← 完整 CRUD
- 基于
BugFree.XCode.Extensions+NewLife.XCodeORM - 支持
Add/Update/Upsert/Delete/Get/Query标准操作 - 泛型约束确保类型安全,支持任意 XCode 实体类
⚙️ 配置控制器
ConfigController<TConfig>— 基于BugFree.Configuration的配置热更新控制器- 开箱即用的配置 Get/Save API 端点
🛡️ 安全中间件
| 中间件 | 功能 |
|---|---|
| IpAccessControlMiddleware | IP 白名单/黑名单控制,支持单 IP、CIDR 网段、IP 范围、动态评估器 |
| SecurityHeaderMiddleware | 自动注入 CSP / HSTS / X-Frame-Options / Permissions-Policy 等安全响应头 |
📋 统一响应模型
Response+ResponseCode— 标准化的 API 响应结构(Success / Fail / 401 / 403 / 500 等)RunTimeContext— 运行时执行统计信息(SQL 次数、耗时、语句列表)
🔧 扩展方法
AddCoreService()— 一键注册 CORS、缓存、响应压缩、API 版本控制AddBugFreeJsonOptions()— 统一 JSON 序列化选项(驼峰命名、忽略 null、默认转换器)GetClientIpAddress()— 支持反向代理的客户端 IP 提取(X-Forwarded-For / X-Real-IP 等)
📐 配置选项(基于 BugFree.Configuration,支持热更新)
| 配置类 | 配置节点 | 说明 |
|---|---|---|
AccessDecisionRulesOptions |
AccessDecisionRules |
IP 访问控制规则 |
CorsOriginRulesOptions |
CorsOriginRules |
CORS 域名白名单 |
HeaderOptions |
Header |
安全响应头配置 |
HeaderSignatureOptions |
HeaderSignature |
请求头签名验证配置 |
PageOptions |
Page |
分页参数 |
RedisOptions |
Redis |
Redis 连接配置 |
目标框架
- .NET 8.0 — 含
Microsoft.AspNetCore.Authentication.JwtBearer+Asp.Versioning.Mvc.ApiExplorer - .NET 10.0 — 同上最新版
安装
dotnet add package BugFree.Controllers.Core
或通过 NuGet 包管理器搜索 BugFree.Controllers.Core。
快速开始
1. 注册核心服务
在 Program.cs 中:
using BugFree.Controllers.Extensions;
var builder = WebApplication.CreateBuilder(args);
// 添加控制器
builder.Services.AddControllers()
.AddBugFreeJsonOptions(); // 统一 JSON 序列化
// 添加 BugFree 核心服务(CORS、缓存、压缩、API 版本控制)
builder.Services.AddCoreService();
2. 创建实体控制器
定义实体和模型(基于 XCode):
// 实体类
public class UserEntity : BugFreeEntity<UserEntity>, IEntity<UserDto>, new()
{
public String Name { get; set; }
public Int32 Age { get; set; }
}
// DTO 类
public class UserDto : IModel
{
public String Name { get; set; }
public Int32 Age { get; set; }
}
继承 EntityController 获得完整 CRUD:
[Area("System")]
public class UserController : EntityController<UserEntity, UserDto>
{
public UserController(IServiceProvider serviceProvider) : base(serviceProvider) { }
// 已自动包含:Add / Update / Upsert / Delete / Get / Query
}
3. 只读控制器
[Area("System")]
public class UserController : ReadOnlyEntityController<UserEntity>
{
public UserController(IServiceProvider serviceProvider) : base(serviceProvider) { }
// 已自动包含:Get / Query
}
4. 配置控制器
[Area("System")]
public class AppConfigController : ConfigController<AppConfigOptions>
{
public AppConfigController(IServiceProvider serviceProvider) : base(serviceProvider) { }
// 已自动包含:Get / Save
}
配置说明
IP 访问控制
在配置文件(如 appsettings.json)中:
{
"AccessDecisionRules": {
"Allows": {
"Enabled": true,
"IpRules": ["192.168.1.0/24", "10.0.0.1-10.0.0.100", "114.114.114.114"]
},
"Denies": {
"Enabled": true,
"IpRules": ["0.0.0.0/8", "10.0.0.5"]
}
}
}
- Allow 优先于 Deny
- 支持:单 IP、CIDR(
192.168.1.0/24)、IP 范围(10.0.0.1-10.0.0.100) - 支持
DynamicEvaluator动态扩展
CORS 域名控制
{
"CorsOriginRules": {
"Enabled": true,
"AllowedDomains": ["example.com", "*.example.com"]
}
}
安全响应头
{
"Header": {
"Enabled": true,
"RemoveHeaders": ["X-AspNet-Version", "X-AspNetMvc-Version"],
"XFrameOptions": "SAMEORIGIN",
"StrictTransportSecurity": "max-age=31536000; includeSubDomains",
"ContentSecurityPolicy": "default-src 'self'; script-src 'self'"
}
}
提示:开发环境默认不启用安全响应头,生产环境自动启用。
响应格式
所有 API 返回统一格式的 Response 结构:
{
"code": 200,
"message": "执行成功",
"data": { ... },
"runtime": null
}
| 响应码 | 说明 |
|---|---|
| 200 | 成功 |
| 201 | 新建或修改成功 |
| 400 | 请求失败 |
| 401 | 未授权 |
| 403 | 禁止访问 |
| 404 | 资源不存在 |
| 500 | 服务器异常 |
依赖
| 包名 | 说明 |
|---|---|
BugFree.Cache |
多级缓存(HybridCache) |
BugFree.XCode.Extensions |
XCode ORM 扩展 |
BugFree.Serialization |
JSON 序列化 |
Microsoft.AspNetCore.Authentication.JwtBearer |
JWT 认证 |
Asp.Versioning.Mvc.ApiExplorer |
API 版本控制 |
项目结构
BugFree.Controllers.Core/
├── Abstractions/ ← 接口定义(IResponseService)
├── Attributes/ ← 特性(SignatureAttribute)
├── Core/
│ ├── Application/ ← 默认响应服务实现
│ ├── Basis/ ← 控制器基类体系
│ ├── Contexts/ ← 属性上下文
│ └── Middleware/ ← 中间件
├── Extensions/ ← 扩展方法(DI 注册、HttpContext)
├── Models/
│ ├── Requests/ ← 请求模型(Page)
│ └── Responses/ ← 响应模型(Response, RunTimeContext)
└── Options/ ← 配置选项
许可
本项目基于 MIT 许可证开源。
| 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 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.
-
net10.0
- Asp.Versioning.Mvc.ApiExplorer (>= 10.0.0)
- BugFree.Cache (>= 1.2.2026.613-beta1618)
- BugFree.XCode.Extensions (>= 1.2.2026.616-beta0958)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 10.0.9)
-
net8.0
- Asp.Versioning.Mvc.ApiExplorer (>= 8.1.1)
- BugFree.Cache (>= 1.2.2026.613-beta1618)
- BugFree.XCode.Extensions (>= 1.2.2026.616-beta0958)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.28)
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.2.2026.624-beta1505 | 0 | 6/24/2026 |
| 1.2.2026.623-beta1237 | 44 | 6/23/2026 |
| 1.2.2026.616-beta1005 | 53 | 6/16/2026 |
| 1.2.2026.615-beta1108 | 52 | 6/15/2026 |
| 1.2.2026.614-beta1951 | 48 | 6/14/2026 |
| 1.2.2026.613-beta1704 | 56 | 6/13/2026 |
| 1.1.2026.612-beta1017 | 47 | 6/12/2026 |
| 1.1.2026.128-beta1507 | 76 | 1/28/2026 |
| 1.1.2026.121-beta1615 | 74 | 1/21/2026 |
| 1.1.2026.121-beta1424 | 69 | 1/21/2026 |
| 1.1.2026.121-beta1413 | 65 | 1/21/2026 |
| 1.1.2026.121-beta1401 | 63 | 1/21/2026 |
| 1.1.2026.121-beta1136 | 67 | 1/21/2026 |
统一 API/MVC 控制器基类体系;5层实体控制器(Empty→Query→Get→ReadOnly→Entity);IP 访问控制中间件;安全响应头注入;统一响应模型;配置热更新控制器。