.NET 使用配置文件

我简单测试了一下json格式配置文件,在这里记录一下,方便以后查看。

需要引用的库:

Microsoft.Extensions.Hosting

创建json格式文件:appsettings.json

在工程文件中包含json文件:

bash 复制代码
	<ItemGroup>
		<Content Include="appsettings.json">
			<CopyToOutputDirectory>Always</CopyToOutputDirectory>
		</Content>
	</ItemGroup>

在使用配置文件的位置可通过如下代码获取配置:

cs 复制代码
        var appSettings = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
        var tradeServerIP = appSettings.GetValue<string>("TradeServerIP");
        var tradeFrontPort = appSettings.GetValue<int>("TradeFrontPort");
相关推荐
追逐时光者17 小时前
一款基于 .NET 开源、免费的数据库自治诊断平台
后端·.net
追逐时光者18 小时前
让 GitHub Copilot 更懂你的 .NET 代码风格:一份精简实用的 Copilot Instructions
后端·.net
Jeff-Tsai18 小时前
Day 13:Session 管理与生命周期
.net·架构设计·erp·definition-driven
Lorenzo5651 天前
扫码点餐里的并发和断网:我在第一个全栈项目里做的几个决定
.net
1-1=02 天前
C# IO和序列化
c#·.net·.netcore
Jeff-Tsai2 天前
Day 12:定义与数据缓存的跨节点失效
.net·架构设计·erp·definition-driven
CSharp精选营2 天前
从控制台到「会动手」的 AI:用 Semantic Kernel 搭应用(小白教程)
c#·.net·ai agent·semantickernel·functioncalling
Jeff-Tsai3 天前
Day 11:数据访问层的连接、Transaction 与查询条件
.net·架构设计·erp·definition-driven
宝桥南山3 天前
.NET 11 - 尝试一下Blazor中的新特性SupplyParameterFromTempData
microsoft·微软·c#·asp.net·.net·.netcore