ASP.NET core Web中使用appsettings.json配置文件的方法
发布时间:2021-01-11 20:23:22 所属栏目:asp.Net 来源:互联网
导读:前言最近在研究把asp.net程序移植到linux上,正好.netcore出来了,就进行了学习。
|
前言 最近在研究把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<T> for your settings.
services.AddOptions();
// Added - Confirms that we have a home for our DemoSettings
services.Configure<DemoSettings>(Configuration.GetSection("DemoSettings"));
}
然后把设置注入进相应的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();
}
}
总结 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程小技巧的支持。 您可能感兴趣的文章:
(编辑:4S站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET网站攻击:如何回应?
- .NET WebApi jsonapi.org支持
- asp.net下使用jquery 的ajax+WebService+json 实现无刷新取
- asp.net – 如何从WCF客户端拦截raw soap request / respon
- asp.net – compilation debug =“true”和发布模式“relea
- 使用ASP.NET AJAX Control Toolkit设置焦点
- asp.net-mvc-3 – 在MVC 3.0中重写Html.BeginForm()并保持不
- asp.net – Intranet / Internet的Windows身份验证
- MVC .Net Cascade在使用EF Code First Approach时删除
- asp.net-mvc – 在asp.net mvc中启动一组未选中的radiobutt
推荐文章
站长推荐
- 如何使用ASP.NET MVC Web API OData链接到Razor中
- asp.net-mvc – View中的意外NullReferenceExcep
- asp.net – 在剃刀中等同于End / Response.End?
- asp.net – 多个用户控件和JavaScript
- asp.net – 我应该使用WebMatrix构建一个真实世界
- ASP.NET TextBox LostFocus事件
- ASP.NET中的应用程序生存期
- ASP.NET Page_Init被解雇了两次!
- asp.net-mvc-4 – .net 4.5 ASP.Net web API JSO
- asp.net-core – 如何在ASP.NET 5中使用“旧”依
热点阅读
