andy-zl通过本文主要向大家介绍了pro asp.net core mvc,vs2010 asp.net web,精通asp.net web api,web程序设计asp.net,简述asp.net的web窗体等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
前言
最近在研究把asp.net程序移植到linux上,正好.net core出来了,就进行了学习。
移植代码基本顺利,但是发现.net core中没有ConfigurationManager,无法读写配置文件,单独写个xml之类的嫌麻烦,就谷歌了下,发现了个方法,遂记录如下,方便以后查找:
方法如下
配置文件结构
public class DemoSettings { public string MainDomain { get; set; } public string SiteName { get; set; } }</div>
appsettings.json中显示效果
appsettings.json
{ "DemoSettings": { "MainDomain": "http://www.mysite.com", "SiteName": "My Main Site" }, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } } }</div>
配置Services
原配置
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); }</div>
自定义
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); // Added - uses IOptions<T> for your settings. services.AddOptions(); // Added - Confirms that we have a home for our DemoSettings services.Configure<DemoSettings>(Configuration.GetSection("DemoSettings")); }</div>
然后把设置注入进相应的Controller后就可以使用了
public class HomeController : Controller { private DemoSettings ConfigSettings { get; set; } public HomeController(IOptions<DemoSettings> settings) { ConfigSettings = settings.Value; } public IActionResult Index() { ViewData["SiteName"] = ConfigSettings.SiteName; return View(); } }</div>
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
</div>您可能想查找下面的文章:
- ASP.NET Core发送邮件的方法
- 在ASP.NET Core 中发送邮件的实现方法(必看篇)
- Asp.net core WebApi 使用Swagger生成帮助页实例
- ASP.NET core Web中使用appsettings.json配置文件的方法
- ASP.NET Core部署前期准备 使用Hyper-V安装Ubuntu Server 16.10
- ASP.NET Core应用中与第三方IoC/DI框架的整合
- ASP.NET Core程序发布到Linux生产环境详解
- 详解ASP.NET Core 网站发布到Linux服务器
- ASP.NET Core全面扫盲贴
- 基于ASP.NET Core数据保护生成验证token示例