您现在的位置是:网站首页> 编程资料编程资料
ASP.NET Core中的配置详解_实用技巧_
2023-05-24
388人已围观
简介 ASP.NET Core中的配置详解_实用技巧_
ASP.NET Core 提供了一个灵活可扩展,基于键值的配置系统. 但是配置系统独立于ASP.NET Core是Microsoft.Extensions 类库的部分. 它可以用于任何类型的应用程序
1、以键-值对的形式读取配置
appsettings.json 文件:
{ "Position": { "Title": "编辑器", "Name": "Joe Smith" }, "MyKey": "My appsettings.json Value", "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*" } 在ConfigureServices方法里面添加如下测试代码:
var myKeyValue = Configuration["MyKey"]; var title = Configuration["Position:Title"]; var name = Configuration["Position:Name"]; var defaultLogLevel = Configuration["Logging:LogLevel:Default"];
2、多环境配置
使用默认配置,EnvironmentVariablesConfigurationProvider 会在读取 appsettings.json、appsettings.Environment.json 和机密管理器后从环境变量键值对加载配置 。 因此,从环境中读取的键值会替代从 appsettings.json、appsettings.Environment.json 和机密管理器中读取的值 。在 launchSettings.json 中设置的环境变量,在 launchSettings.json 中设置的环境变量将替代在系统环境中设置的变量。
3、读取结构化的配置数据
添加一个类 TestSubSectionConfig 对应于配置文件中的 subsection 节点
public class TestSubSectionConfig { public string SubOption1 { get; set; } public string SubOption2 { get; set; } } 在ConfigureServices方法里面添加如下测试代码:
//使用GetSection解析配置文件的节 var subsectionOptions = Configuration.GetSection("subsection").Get(); var suboption2 = subsectionOptions.SubOption2; Console.WriteLine($"subsection:suboption2: {suboption2}"); 如果需要在Controller里面使用,可以通过依赖注入的方式:
在ConfigureServices里面注册配置项。
public void ConfigureServices(IServiceCollection services) { //注册配置到服务容器 services.Configure(Configuration.GetSection("subsection")); //var subsectionOptions = Configuration.GetSection("subsection").Get(); //services.Configure(options => //{ // options.SubOption1 = subsectionOptions["suboption1"]; // options.SubOption2 = subsectionOptions["suboption2"]; // }); } public class HomeController : Controller { private TestSubSectionConfig _subSectionConfig; private ILogger _logger; public HomeController(IOptions option, ILogger logger) { _subSectionConfig = option.Value; _logger = logger; } public IActionResult Index() { _logger.LogInformation($"SubOption1: {_subSectionConfig.SubOption1}"); _logger.LogInformation($"SubOption2: {_subSectionConfig.SubOption2}"); return View(); } } 到此这篇关于ASP.NET Core中的配置详解的文章就介绍到这了,更多相关ASP.NET Core配置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
- 在 ASP.Net Core 中使用 MiniProfiler的方法
- ASP.NET Core MVC解决控制器同名Action请求不明确的问题
- Asp.Net Core 调用第三方Open API查询物流数据的示例
- Asp.Net Core中创建多DbContext并迁移到数据库的步骤
- 如何在Asp.Net Core中集成Refit
- ASP.NET Core WebApi版本控制的实现
- ASP.NET Core对不同类型的用户进行区别限流详解
- 详解如何在ASP.NET Core中编写高效的控制器
- 详解如何在ASP.NET Core中使用IHttpClientFactory
- ASP.NET Core 使用Cookie验证身份的示例代码
- 如何在ASP.Net Core使用分布式缓存的实现
- 详解如何在ASP.NET Core Web API中以三种方式返回数据
- 详解如何在ASP.NET Core中使用Route特性
- 如何在Asp.Net Core中集成ABP Dapper
相关内容
- .NET使用DinkToPdf将HTML转成PDF的示例代码_实用技巧_
- 在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle数据库_实用技巧_
- .NET使用MailKit进行邮件处理的方法步骤_实用技巧_
- .NET+PostgreSQL实践与避坑指南(推荐)_实用技巧_
- .NET 操作 PostgreSQL遇到的问题_实用技巧_
- 详解如何在ASP.NET Core中使用Route特性_实用技巧_
- ASP.NET Core中如何实现重定向详解_实用技巧_
- 如何在ASP.NET Core中使用Session的示例代码_实用技巧_
- Asp.Net Core中发送Email的完整步骤_实用技巧_
- .NET Core 处理 WebAPI JSON 返回烦人的null为空_实用技巧_
