.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");
相关推荐
java156550579702 小时前
深入浅出 SQLSugar:快速掌握高效 .NET ORM 框架
.net
晴天_QingTian5 小时前
.NET8入门:13.ASP.NET Core MVC
asp.net·mvc·.net
zhang_adrian6 小时前
.Net 9下使用Tensorflow.net---DNN_Eager
tensorflow·.net·dnn
升讯威在线客服系统7 小时前
如何通过 Docker 在没有域名的情况下快速上线客服系统
java·运维·前端·python·docker·容器·.net
五VV20 小时前
Note25021902_TIA Portal V18 WinCC BCA Ed 需要.NET 3.5 SP1
.net
code_shenbing1 天前
.NET MVC实现电影票管理
mvc·.net
shepherd枸杞泡茶2 天前
第3章 3.3日志 .NET Core日志 NLog使用教程
c#·asp.net·.net·.netcore
时光追逐者2 天前
推荐几款开源免费的 .NET MAUI 组件库
microsoft·开源·c#·.net·.net core·maui
三天不学习2 天前
.Net面试宝典【刷题系列】
面试·职场和发展·.net
JosieBook2 天前
【.NET全栈】.NET包含的所有技术
.net