첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ appsettings.json

{
    "Test" :
    {
        "Key1" : "Key One",
        "Key2" : 40,
        "Key3" : 30
    },
    "Logging"  :
    {
        "LogLevel" :
        {
            "Default"                    : "Information",
            "Microsoft"                  : "Warning",
            "Microsoft.Hosting.Lifetime" : "Information"
        }
    },
    "AllowedHosts" : "*"
}

 

728x90

 

▶ Models/TestOption.cs

using System.ComponentModel.DataAnnotations;

namespace TestProject.Models
{
    /// <summary>
    /// 스타일 옵션
    /// </summary>
    public class TestOption
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 키 1 - Key1

        /// <summary>
        /// 키 1
        /// </summary>
        [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$")]
        public string Key1 { get; set; }

        #endregion
        #region 키 2 - Key2

        /// <summary>
        /// 키 2
        /// </summary>
        [Range(0, 1000, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
        public int Key2 { get; set; }

        #endregion
        #region 키 3 - Key3

        /// <summary>
        /// 키 3
        /// </summary>
        public int Key3 { get; set; }

        #endregion
    }
}

 

300x250

 

▶ Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using TestProject.Models;

namespace TestProject
{
    /// <summary>
    /// 시작
    /// </summary>
    public class Startup
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 구성 - Configuration

        /// <summary>
        /// 구성
        /// </summary>
        public IConfiguration Configuration { get; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - Startup(configuration)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="configuration">구성</param>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 서비스 컬렉션 구성하기 - ConfigureServices(services)

        /// <summary>
        /// 서비스 컬렉션 구성하기
        /// </summary>
        /// <param name="services">서비스 컬렉션</param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions<TestOption>()
                .Bind(Configuration.GetSection("Test"))
                .ValidateDataAnnotations()
                .Validate
                (
                    testOption =>
                    {
                        if(testOption.Key2 != 0)
                        {
                            return testOption.Key3 > testOption.Key2;
                        }

                        return true;
                    },
                    "Key3 must be > than Key2."
                );

            services.AddControllersWithViews();
        }

        #endregion
        #region 구성하기 - Configure(app, environment)

        /// <summary>
        /// 구성하기
        /// </summary>
        /// <param name="app">애플리케이션 빌더</param>
        /// <param name="environment">웹 호스트 환경</param>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment environment)
        {
            if(environment.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");

                app.UseHsts();
            }

            app.UseHttpsRedirection();

            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints
            (
                endpoints =>
                {
                    endpoints.MapControllerRoute
                    (
                        name    : "default",
                        pattern : "{controller=Home}/{action=Index}/{id?}"
                    );
                }
            );
        }

        #endregion
    }
}

 

▶ Controllers/TestController.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Text;

using TestProject.Models;

namespace TestProject.Controllers
{
    /// <summary>
    /// 테스트 컨트롤러
    /// </summary>
    public class TestController : Controller
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 로그 기록기
        /// </summary>
        private readonly ILogger<TestController> logger;

        /// <summary>
        /// 옵션
        /// </summary>
        private readonly IOptions<TestOption> options;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - TestController(logger, options)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="logger">로그 기록기</param>
        /// <param name="options">옵션</param>
        public TestController(ILogger<TestController> logger, IOptions<TestOption> options)
        {
            this.logger  = logger;
            this.options = options;

            try
            {
                TestOption testOption = this.options.Value;
           
            }
            catch(OptionsValidationException exception)
            {
                foreach(string failure in exception.Failures)
                {
                    this.logger.LogError(failure);
                }
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 인덱스 페이지 처리하기 - Index()

        /// <summary>
        /// 인덱스 페이지 처리하기
        /// </summary>
        /// <returns>액션 결과</returns>
        public IActionResult Index()
        {
            StringBuilder stringBuilder = new StringBuilder();

            try
            {
                stringBuilder.AppendLine($"Key1 : { this.options.Value.Key1}");
                stringBuilder.AppendLine($"Key2 : { this.options.Value.Key2}");
                stringBuilder.AppendLine($"Key3 : { this.options.Value.Key3}");
            }
            catch(OptionsValidationException exception)
            {
                return Content(exception.Message);
            }

            return Content(stringBuilder.ToString());
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요