最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET core Web中使用appsettings.json配置文件的方法

 更新時(shí)間:2017年04月23日 14:50:22   作者:andy-zl  
這篇文章主要給大家介紹了在ASP.NET core Web中使用appsettings.json配置文件的方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看看吧。

前言

最近在研究把a(bǔ)sp.net程序移植到linux上,正好.net core出來(lái)了,就進(jìn)行了學(xué)習(xí)。

移植代碼基本順利,但是發(fā)現(xiàn).net core中沒(méi)有ConfigurationManager,無(wú)法讀寫(xiě)配置文件,單獨(dú)寫(xiě)個(gè)xml之類(lèi)的嫌麻煩,就谷歌了下,發(fā)現(xiàn)了個(gè)方法,遂記錄如下,方便以后查找:

方法如下

配置文件結(jié)構(gòu)

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"));
}

然后把設(shè)置注入進(jìn)相應(yīng)的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();
 }
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

甘孜| 九寨沟县| 南昌县| 星子县| 太保市| 康保县| 岱山县| 新巴尔虎右旗| 辽阳市| 青海省| 鲁甸县| 青川县| 开平市| 红河县| 将乐县| 手游| 中卫市| 隆安县| 云和县| 远安县| 乌拉特中旗| 永嘉县| 阿鲁科尔沁旗| 姜堰市| 桓仁| 旬邑县| 武强县| 平遥县| 绥棱县| 罗山县| 镇坪县| 湖南省| 东莞市| 大埔区| 古蔺县| 林西县| 兴山县| 瓦房店市| 定结县| 涿鹿县| 疏勒县|