您现在的位置是:网站首页> 编程资料编程资料
ASP.NET core Web中使用appsettings.json配置文件的方法_实用技巧_
                     2023-05-24
                160人已围观
                
                2023-05-24
                160人已围观
            
简介 ASP.NET core Web中使用appsettings.json配置文件的方法_实用技巧_
前言
最近在研究把asp.net程序移植到linux上,正好.net core出来了,就进行了学习。
移植代码基本顺利,但是发现.net core中没有ConfigurationManager,无法读写配置文件,单独写个xml之类的嫌麻烦,就谷歌了下,发现了个方法,遂记录如下,方便以后查找:
方法如下
配置文件结构
 public class DemoSettings { public string MainDomain { get; set; } public string SiteName { get; set; } }appsettings.json中显示效果
appsettings.json
 { "DemoSettings": { "MainDomain": "http://www.mysite.com", "SiteName": "My Main Site" }, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } } }配置Services
原配置
 public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); }自定义
 public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); // Added - uses IOptions for your settings. services.AddOptions(); // Added - Confirms that we have a home for our DemoSettings services.Configure(Configuration.GetSection("DemoSettings")); }  然后把设置注入进相应的Controller后就可以使用了
 public class HomeController : Controller { private DemoSettings ConfigSettings { get; set; } public HomeController(IOptions settings) { ConfigSettings = settings.Value; } public IActionResult Index() { ViewData["SiteName"] = ConfigSettings.SiteName; return View(); } } 总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
您可能感兴趣的文章:
                
                
相关内容
- SqlDataReader生成动态Lambda表达式_实用技巧_
- VS2017做为Unity3D的脚本编辑器需要的最精简组件_实用技巧_
- Visual studio 2017如何发布dotnet core到docker_实用技巧_
- WPF中button按钮同时点击多次触发click解决方法_实用技巧_
- MVC页面之间参数传递解析_实用技巧_
- VS2015下OpenGL库配置教程_实用技巧_
- Visual Studio 2017安装失败的解决方法_实用技巧_
- VS2017添加EF的MVC控制器报错的解决方法_实用技巧_
- ASP.NET Core部署前期准备 使用Hyper-V安装Ubuntu Server 16.10_实用技巧_
- VS2017 Cordova Ionic2 移动开发环境搭建教程_实用技巧_
 
                                
                                                         
                                
                                                         
                                
                                                         
 
    