■ ConfigurationBinder 클래스의 Get 확장 메소드를 사용해 배열을 바인딩하는 방법을 보여준다.
▶ appsettings.json
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 |
{ "ProgramList" : { "EntryList" : { "0" : "notepad.exe", "1" : "excel.exe", "2" : "calculator.exe", "4" : "paint.exe", "5" : "powerpoint.exe" } }, "Logging" : { "LogLevel" : { "Default" : "Information", "Microsoft" : "Warning", "Microsoft.Hosting.Lifetime" : "Information" } }, "AllowedHosts" : "*" } |
▶ Models/ProgramList.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
namespace TestProject.Models { /// <summary> /// 프로그램 리스트 /// </summary> public class ProgramList { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 엔트리 리스트 - EntryList /// <summary> /// 엔트리 리스트 /// </summary> public string[] EntryList { get; set; } #endregion } } |
▶ 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 69 70 71 |
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System.Text; using TestProject.Models; 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() { ProgramList programList = this.configuration.GetSection("ProgramList").Get<ProgramList>(); StringBuilder stringBuilder = new StringBuilder(); for(int i = 0; i < programList.EntryList.Length; i++) { string program = programList.EntryList[i]; stringBuilder.AppendLine($"{i} : {program}"); } return Content(stringBuilder.ToString()); } #endregion } } |
※ 구성 키에서 배열 인덱스를 사용하여 배열을 개체에 바인딩할 수 있도록 지원한다.
※ 숫자 키 세그먼트를 노출하는 모든 배열 형식은 POCO 클래스 배열에 배열을 바인딩할 수 있다.
※ 구성 바인더는 null 값을 바인딩하거나 바인딩된 개체에 null 항목을 만들 수 없다.