VOOZH about

URL: https://www.nuget.org/packages/BugFree.Controllers.Core/

⇱ NuGet Gallery | BugFree.Controllers.Core 1.2.2026.616-beta1005




👁 Image
BugFree.Controllers.Core 1.2.2026.616-beta1005

This is a prerelease version of BugFree.Controllers.Core.
dotnet add package BugFree.Controllers.Core --version 1.2.2026.616-beta1005
 
 
NuGet\Install-Package BugFree.Controllers.Core -Version 1.2.2026.616-beta1005
 
 
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.616-beta1005" />
 
 
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.616-beta1005" />
 
Directory.Packages.props
<PackageReference Include="BugFree.Controllers.Core" />
 
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 BugFree.Controllers.Core --version 1.2.2026.616-beta1005
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: BugFree.Controllers.Core, 1.2.2026.616-beta1005"
 
 
#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.616-beta1005
 
 
#: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.616-beta1005&prerelease
 
Install as a Cake Addin
#tool nuget:?package=BugFree.Controllers.Core&version=1.2.2026.616-beta1005&prerelease
 
Install as a Cake Tool
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 基础设施。

👁 NuGet


特性

📦 统一控制器基类

  • BugFreeController — 继承 ControllerBase,内置 Success() / Fail() / Unauthorized() 快捷响应方法
  • 统一 IResponseService 可替换策略,支持自定义响应构建行为

🧩 实体控制器层次体系

BugFreeController
 └── EmptyEntityController&lt;TEntity&gt; ← 实体工厂初始化
 └── QueryEntityController&lt;TEntity&gt; ← 列表查询 (Query)
 └── GetEntityController&lt;TEntity&gt; ← 单实体查询 (Get)
 └── ReadOnlyEntityController&lt;TEntity&gt; ← 只读
 └── EntityController&lt;TEntity,TModel&gt; ← 完整 CRUD
  • 基于 BugFree.XCode.Extensions + NewLife.XCode ORM
  • 支持 Add / Update / Upsert / Delete / Get / Query 标准操作
  • 泛型约束确保类型安全,支持任意 XCode 实体类

⚙️ 配置控制器

  • ConfigController&lt;TConfig&gt; — 基于 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&lt;UserEntity&gt;, IEntity&lt;UserDto&gt;, 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&lt;UserEntity, UserDto&gt;
{
 public UserController(IServiceProvider serviceProvider) : base(serviceProvider) { }

 // 已自动包含:Add / Update / Upsert / Delete / Get / Query
}

3. 只读控制器

[Area("System")]
public class UserController : ReadOnlyEntityController&lt;UserEntity&gt;
{
 public UserController(IServiceProvider serviceProvider) : base(serviceProvider) { }

 // 已自动包含:Get / Query
}

4. 配置控制器

[Area("System")]
public class AppConfigController : ConfigController&lt;AppConfigOptions&gt;
{
 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 net8.0 is compatible.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 is compatible.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows 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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

统一 API/MVC 控制器基类体系;5层实体控制器(Empty→Query→Get→ReadOnly→Entity);IP 访问控制中间件;安全响应头注入;统一响应模型;配置热更新控制器。