[C#/ASP.NET MVC/.NETCORE] IConfiguration 인터페이스 : GetChildren 메소드를 사용해 자식 구성 하위 섹션 구하기
C#/ASP.NET MVC 2020. 10. 22. 12:33728x90
728x170
▶ appsettings.json
{
"TestKey" : "TestValue1",
"Position" :
{
"Title" : "Editor1",
"Name" : "Smith1"
},
"Logging" :
{
"LogLevel" :
{
"Default" : "Information",
"Microsoft" : "Warning",
"Microsoft.Hosting.Lifetime" : "Information"
}
},
"AllowedHosts" : "*"
}
728x90
▶ Controllers/TestController.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Text;
namespace TestProject.Controllers
{
/// <summary>
/// 테스트 컨트롤러
/// </summary>
public class TestController : Controller
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 구성
/// </summary>
private IConfiguration configuration;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - TestController(configuration)
/// <summary>
/// 생성자
/// </summary>
/// <param name="configuration">구성</param>
public TestController(IConfiguration configuration)
{
this.configuration = configuration;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 인덱스 페이지 처리하기 - Index()
/// <summary>
/// 인덱스 페이지 처리하기
/// </summary>
/// <returns>액션 결과</returns>
public IActionResult Index()
{
IConfigurationSection section = this.configuration.GetSection("Position");
IEnumerable<IConfigurationSection> childSectionEnumerable = section.GetChildren();
StringBuilder stringBuilder = new StringBuilder();
foreach(IConfigurationSection childSection in childSectionEnumerable)
{
stringBuilder.AppendLine($"{childSection.Key} : {section[childSection.Key]}");
}
return Content(stringBuilder.ToString());
}
#endregion
}
}
728x90
그리드형(광고전용)