ApexCore.AspNetCore 1.0.0

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

ApexCore.AspNetCore 层手册

概览

ApexCore.AspNetCore 提供 Web 层接入:统一响应包装、统一异常处理、基于 Claims 的当前用户/租户实现,以及相关的服务注册与中间件启用方法。

业务场景

  • 统一 API 返回结构,避免前端/客户端解析不一致。
  • 标准化异常到 API 错误码与 HTTP 状态码。
  • 统一用户/租户上下文的获取方式。

核心能力

  • AddApexCoreAspNetCore:服务注册入口。
  • ApiResultFilter:统一响应包装。
  • DisableApiResultAttribute:禁用包装的特性。
  • ExceptionHandlingMiddleware:异常统一处理。
  • AspNetCoreCurrentUser<TUserId> / AspNetCoreCurrentTenant<TTenantId>:Claims 解析。
  • ApexCoreAspNetCoreOptions:AspNetCore 相关配置。

如何使用

1. 注册服务

services.AddApexCoreAspNetCore(options =>
{
    options.WrapResponse = true;
    options.IncludeExceptionDetails = false;
    options.ConcurrencyErrorCode = "CONCURRENCY";
    options.ValidationErrorCode = "VALIDATION";
    options.DefaultErrorCode = "SERVER_ERROR";
});

2. 启用中间件

app.UseApexCoreAspNetCore();

3. 禁用统一包装(可选)

[DisableApiResult]
public IActionResult Raw() => Ok("raw response");

实现逻辑

  • ApiResultFilter
    • WrapResponse=false 或命中 DisableApiResultAttribute,跳过包装。
    • ObjectResult/JsonResult 统一包裹为 ApiResult<T>
    • EmptyResult 转为 ApiResult.Ok()
    • 文件/重定向/挑战等结果默认跳过。
  • ExceptionHandlingMiddleware
    • 捕获异常后输出 JSON 格式 ApiResult.Fail(...)
    • ApexCoreConcurrencyException → HTTP 409 + ConcurrencyErrorCode
    • ValidationException → HTTP 400 + ValidationErrorCode
    • 其他异常 → HTTP 500 + DefaultErrorCode
  • Claims 解析
    • AspNetCoreCurrentUser/Tenant 按配置的 Claim 类型顺序取值并转换类型。

关键配置

ApexCoreAspNetCoreOptions 默认值:

  • WrapResponse = true
  • IncludeExceptionDetails = false
  • UserIdClaimTypes = [NameIdentifier, "sub", "userId"]
  • UserNameClaimTypes = [Name, "userName"]
  • TenantIdClaimTypes = ["tenant_id", "tenantId"]
  • ConcurrencyErrorCode = "CONCURRENCY"
  • ValidationErrorCode = "VALIDATION"
  • DefaultErrorCode = "SERVER_ERROR"

扩展点

  • 替换 AspNetCoreCurrentUser/Tenant 以对接自定义鉴权体系。
  • 自定义 ApiResultFilterExceptionHandlingMiddleware 行为。

注意事项与边界

  • 若响应已开始(context.Response.HasStarted),中间件会重新抛出异常。
  • ApiResultFilter 对特定结果类型默认跳过包装(如 FileResult)。

示例

[HttpGet]
public ApiResult<string> GetHello()
{
    return ApiResult<string>.Ok("hello");
}
Product Compatible and additional computed target framework versions.
.NET 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.

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
9.0.1 97 5/12/2026
1.0.0 149 2/7/2026
1.0.0-net9 96 4/23/2026