■ IConfiguration 인터페이스의 GetSection 메소드를 사용해 구성 하위 섹션을 구하는 방법을 보여준다.
▶ appsettings.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
{ "TestKey" : "TestValue1", "Position" : { "Title" : "Editor1", "Name" : "Smith1" }, "Logging" : { "LogLevel" : { "Default" : "Information", "Microsoft" : "Warning", "Microsoft.Hosting.Lifetime" : "Information" } }, "AllowedHosts" : "*" } |
▶ Controllers/TestController.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; 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"); string title = section["Title"]; string name = section["Name" ]; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"Position / Title : {title}"); stringBuilder.AppendLine($"Position / Name : {name}" ); return Content(stringBuilder.ToString()); } #endregion } } |