첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ Settings/MainSettings.cs

namespace TestProject.Settings
{
    /// <summary>
    /// 메인 설정
    /// </summary>
    public class MainSettings
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 애플리케이션 ID - ApplicationID

        /// <summary>
        /// 애플리케이션 ID
        /// </summary>
        public int ApplicationID { get; set; }

        #endregion
        #region 애플리케이션명 - ApplicationName

        /// <summary>
        /// 애플리케이션명
        /// </summary>
        public string ApplicationName { get; set; }

        #endregion
    }
}

 

728x90

 

▶ Settings/MainSettings.json

{
    "MainSettings" :
    {
        "ApplicationID"   : 100,
        "ApplicationName" : "TestApplication"
    }
}

※ 상기 파일의 속성을 아래와 같이 설정한다.

항목
빌드 작업 없음
출력 디렉터리로 복사 새 버전이면 복사

 

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.Settings;

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.AddControllersWithViews();

            IConfigurationRoot configurationRoot = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("Settings\\MainSettings.json").Build();

            services.Configure<MainSettings>(configurationRoot.GetSection("MainSettings"));
        }

        #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.Options;

using TestProject.Settings;

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

        #region Field

        /// <summary>
        /// 메인 설정
        /// </summary>
        private MainSettings settings;

        #endregion

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

        #region 생성자 - TestController(options)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="options">옵션</param>
        public TestController(IOptions<MainSettings> options)
        {
            this.settings = options.Value;
        }

        #endregion

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

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

        /// <summary>
        /// 인덱스 페이지 처리하기
        /// </summary>
        /// <returns>액션 결과</returns>
        public IActionResult Index()
        {
            ViewBag.ApplicationID   = this.settings.ApplicationID;
            ViewBag.ApplicationName = this.settings.ApplicationName;

            return View();
        }

        #endregion
    }
}

 

▶ Views/Test/Index.cshtml

@{ Layout = null; }
<p>JsonConfigurationExtensions 클래스 : AddJsonFile 확장 메소드를 사용해 환경 설정 파일 로드하기</p>
<p>Application ID : @ViewBag.ApplicationID</p>
<p>Application Name : @ViewBag.ApplicationName</p>
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요