■ XmlConfigurationExtensions 클래스의 AddXmlFile 확장 메소드를 사용해 XML 구성 공급자를 등록하는 방법을 보여준다.
▶ sample.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <TestKey>TestValue2</TestKey> <Position> <Title>Editor2</Title> <Name>Smith2</Name> </Position> <Logging> <LogLevel> <Default>Information</Default> <Microsoft>Warning</Microsoft> </LogLevel> </Logging> </configuration> |
▶ Program.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 72 73 74 75 76 77 78 |
using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; namespace TestProject { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 프로그램 시작하기 - Main(argumentArray) /// <summary> /// 프로그램 시작하기 /// </summary> /// <param name="argumentArray">인자 배열</param> public static void Main(string[] argumentArray) { CreateHostBuilder(argumentArray).Build().Run(); } #endregion #region 호스트 빌더 생성하기 - CreateHostBuilder(argumentArray) /// <summary> /// 호스트 빌더 생성하기 /// </summary> /// <param name="argumentArray">인자 배열</param> /// <returns>호스트 빌더</returns> public static IHostBuilder CreateHostBuilder(string[] argumentArray) => Host.CreateDefaultBuilder(argumentArray) .ConfigureAppConfiguration ( (context, configuration) => { var environment = context.HostingEnvironment; configuration.AddXmlFile ( "sample.xml", optional : true, reloadOnChange : true ); configuration.AddXmlFile ( $"sample.{environment.EnvironmentName}.xml", optional : true, reloadOnChange : true ); configuration.AddEnvironmentVariables(); if(argumentArray != null) { configuration.AddCommandLine(argumentArray); } } ) .ConfigureWebHostDefaults ( builder => { builder.UseStartup<Startup>(); } ); } #endregion } } |
▶ 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 69 70 |
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() { string testKey = this.configuration["TestKey" ]; string title = this.configuration["Position:Title" ]; string name = this.configuration["Position:Name" ]; string loggingLogLevelDefault = this.configuration["Logging:LogLevel:Default"]; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"TestKey : {testKey}" ); stringBuilder.AppendLine($"Position / Title : {title}" ); stringBuilder.AppendLine($"Position / Name : {name}" ); stringBuilder.AppendLine($"Logging / LogLevel / Default : {loggingLogLevelDefault}"); return Content(stringBuilder.ToString()); } #endregion } } |