当前位置:网站首页>.net core 中配置文件映射到类
.net core 中配置文件映射到类
2022-04-22 18:16:00 【小兜全糖(Cx)】
- 编写appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Custom": {
"key": "123",
"ser": "456"
}
}
- 编写映射类
namespace WebApplication1
{
public class Config
{
public string key {
get; set; }
public string ser {
get; set; }
}
}
- 注入配置
builder.Services.AddScoped<Config>();
builder.Services.Configure<Config>(
builder.Configuration.GetSection("custom")
);
- config的注入与使用
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace WebApplication1.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
private readonly IOptionsSnapshot<Config> _config;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IOptionsSnapshot<Config> config)
{
_logger = logger;
_config = config;
var ss = _config.Value.key;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
版权声明
本文为[小兜全糖(Cx)]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43632687/article/details/122695897
边栏推荐
- [Lane] ultra fast lane detection (2) custom model test
- 电脑硬件都有哪些?
- 【驱动】TX2移植EC20启动模块
- 你知道你的手机上有多少传感器吗?
- leetcode 108:将有序数组转换为二叉搜索树
- leetcode 111:二叉树的最小深度
- Leetcode 108: convert an ordered array into a binary search tree
- [Lane] ultra fast lane detection (1) custom dataset training
- [2021] Tencent autumn recruitment technology post programming arrangement supermarket
- How to select the mobile phone running memory?
猜你喜欢
![B树[概念]](/img/de/e52df3061a1615291de9b29c7b5c5c.png)
B树[概念]

Applets -- components

Golang gin websocket problem

Transformation operator

秒雲助力中電科32所發布“基於擬態應用集成框架的SaaS雲管理平臺解决方案”

图像的卷积——【torch学习笔记】

秒云助力中电科32所发布“基于拟态应用集成框架的SaaS云管理平台解决方案”

小程序----组件

【论文泛读】Easing Embedding Learning by Comprehensive Transcription of Heterogeneous Information Networks

国产芯片DP9637-K总线收发器替代L9637D芯片和SI9241
随机推荐
还弄不懂相对路径和绝对路径,这篇文章带你简单剖析
【思考与进步】:关于自己的遗憾
因索引合并导致的MySQL死锁分析与解决实战!
电脑上怎么快速切换显示不同的软件界面
leetcode-470. 用 Rand7() 实现 Rand10()
National information exchange model (NIEM) operation manual
Examples of comprehensive application of DOM -- click plus one and minus one
This API hub is powerful. It contains open APIs such as nailing enterprise wechat, and can be debugged directly!
广东水泥数据概况
最近学习感悟
[Lane] ultra fast lane detection (2) custom model test
Zeromq: Event Monitoring
目前国产电脑硬件的现状是怎样的?
知乎热议:浙大读博八年现靠送外卖赚钱
Domestic chip dp9637-k bus transceiver replaces l9637d chip and si9241
DLL的封装及调用
In 2022, it is said on the Internet that Apple's upcoming new models iPhone 14 pro and iPhone 14 Pro Max will be a new screen shape, not a banged screen. Do you expect the iPhone 14 with a new screen
sh文件内容启动文件
Soft test high item notes | PERT three-point estimation
[fundamentals of interface testing] Chapter 10 | explain the pre script of postman request and its working principle in detail