Ybd.AbpLite.Configuration 1.0.0

dotnet add package Ybd.AbpLite.Configuration --version 1.0.0
                    
NuGet\Install-Package Ybd.AbpLite.Configuration -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="Ybd.AbpLite.Configuration" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ybd.AbpLite.Configuration" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Ybd.AbpLite.Configuration" />
                    
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 Ybd.AbpLite.Configuration --version 1.0.0
                    
#r "nuget: Ybd.AbpLite.Configuration, 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 Ybd.AbpLite.Configuration@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=Ybd.AbpLite.Configuration&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Ybd.AbpLite.Configuration&version=1.0.0
                    
Install as a Cake Tool

Ybd.AbpLite.Configuration

把配置按文件拆分,并自动加载。appsettings.json 只放基础项;对象存储、支付、短信等各放各的 json,新增一个文件就生效,不用改代码。

不依赖 ABP,任何 .NET 8 项目(Web / 控制台 / Worker)都能用。

安装

dotnet add package Ybd.AbpLite.Configuration

使用

Web 项目(在 WebApplication.CreateBuilder 之后):

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddSplitJsonFiles(
    builder.Environment.EnvironmentName,
    builder.Environment.ContentRootPath,
    args);

控制台项目(放在 appsettings 之后、环境变量之前):

var configuration = new ConfigurationBuilder()
    .SetBasePath(AppContext.BaseDirectory)
    .AddJsonFile("appsettings.json")
    .AddSplitJsonFiles(environment, AppContext.BaseDirectory, args)
    .AddEnvironmentVariables()
    .AddCommandLine(args)
    .Build();

之后在应用目录下建 Configs/ 目录,放进任意 *.json(含子目录)即可:

Configs/
├── oss.json      { "AliyunOss": { "Bucket": "..." } }
├── pay.json      { "Pay": { "MchId": "..." } }
└── jwt.json

文件需要随程序一起部署,请在 csproj 里加一行,让它们复制到输出目录和发布目录:<Content Update="Configs\**\*.json" CopyToOutputDirectory="PreserveNewest" />。

启动后可以查看实际加载了哪些文件:

var files = builder.Configuration.GetSplitConfigurationFiles();   // 完整路径,按加载顺序

覆盖优先级

后面的覆盖前面的:

appsettings.json < appsettings.{环境}.json < 拆分文件 < 用户机密 < 环境变量 < 命令行

拆分文件内部:普通文件(按路径排序)< 环境后缀文件(如 oss.Development.json)。不同文件不要定义同一个键,否则结果取决于文件名顺序。

选项:ConfigFiles

写在 appsettings.json(也可以用环境变量 / 命令行,例如 ConfigFiles__Directory=/etc/myapp):

"ConfigFiles": {
  "Enabled": true,
  "Directory": "Configs",
  "AutoDiscover": true,
  "Files": [],
  "ReloadOnChange": false,
  "EnvironmentNames": []
}
键 默认 说明
Enabled true 总开关
Directory Configs 目录。相对路径相对于 basePath(Web 项目为内容根目录),也可以写绝对路径;目录不存在不算错
AutoDiscover true 自动加载目录(含子目录)下所有 *.json
Files [] 显式指定的文件(相对 Directory 或绝对路径)。指定了但不存在会直接报错,避免拼写错误被悄悄忽略
ReloadOnChange false 文件变化时重新加载。IOptions<T> 不会随之变化,只有 IOptionsMonitor<T> 会
EnvironmentNames [] 额外的环境名(见下)

不想用 appsettings.json 里的配置节,也可以直接传入:

builder.Configuration.AddSplitJsonFiles(environment, basePath, options: new SplitConfigurationOptions
{
    Directory = "config",
    AutoDiscover = false,
    Files = { "jwt.json", "pay.json" }
});

按环境区分

文件名形如 oss.Production.json,只在对应环境加载,并覆盖同目录的普通文件 oss.json:

Configs/
├── oss.json                 # 所有环境
├── oss.Development.json     # 只在 Development,覆盖 oss.json 的同名键
└── oss.Production.json      # 只在 Production
  • 内置识别 Development、Staging、Production、Test。用了别的环境名(如 Uat)请加到 EnvironmentNames,否则 oss.Uat.json 会被当成普通文件在所有环境加载。
  • 文件名本来就带点的(如 oss.aliyun.json):aliyun 不是环境名,按普通文件处理。
  • 环境名取调用时传入的 environmentName;为空时依次读取 DOTNET_ENVIRONMENT、ASPNETCORE_ENVIRONMENT,都没有则为 Production。

注意

  • JSON 文件允许 // 注释和末尾逗号(.NET 配置系统的行为),可以在配置文件里写说明。
  • 只支持 JSON 文件;某个文件不是合法 JSON 时,启动会报错并指出是哪个文件。
  • 想要 appsettings.json 里的 ConfigFiles 能被读到,它必须在调用 AddSplitJsonFiles 之前已加入构建器(Web 项目默认满足)。

许可

MIT

Product 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.

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.0.0 116 10/17/2026