📌 ASP.net Core 기본구조
![](/assets/img/dotnet/dotnetlogo.png)
해당 포스트는 MicroSoft - Docs/.Net/ASP.net Core의 기술문서 정보를 기반으로 작성하였습니다.
ASP .net Core 기본구조
1️⃣ Program.cs
ASP .net Core를 기반으로 만든 프로젝트는 Program.cs
에 의해 흐름이 제어된다.
(Program.cs가 포함된 프로젝트: Razor Pages, MVC, 컨트롤러가 있는 Web API, 최소 웹 API)
공식문서에서 기술된 Program.cs
의 역할은 다음과 같다.
- 앱에서 요구하는 서비스가 구성됩니다.
- 앱의 요청 처리 파이프라인이 일련의 미들웨어 구성 요소로 정의됩니다.
다음 예시코드를 보자.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// 서비스를 추가하는 부분
builder.Services.AddRazorPages();
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthorization();
app.MapGet("/hi", () => "Hello!");
app.MapDefaultControllerRoute();
app.MapRazorPages();
app.Run();
builder
빌더는 응용 프로그램에 미들웨어를 추가해주는 객체이다.
빌더를 통해 RazorPages
와 ControllersWithViews
서비스를 추가 후 빌드했다.
위와 같은 서비스를 사용할 수 있도록 DI(종속성 주입) 프레임워크가 포함되어있다.
WebApplication.CreateBuilder(args);
에서 DI컨테이너에 추가된다.
미들웨어
서버에 요청이 들어올 때, 여러 미들웨어를 호출한 후, 요청을 종료한다.
요청에 대한 응답을 처리하는 파이프라인 형식을 갖추고 있다. 미들웨어는 파이프라인으로 조립된다.
요청 파이프라인을 빌드하는 것을 요청 대리자라고 한다. 요청 대리자는 각 HTTP 요청을 처리한다.
요청 대리자는 Run, Map 및 Use 확장 메서드를 사용하여 구성된다.
Use
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Use(async (context, next) =>
{
// Do work that can write to the Response.
await next.Invoke();
// Do logging or other work that doesn't write to the Response.
});
app.Run(async context =>
{
await context.Response.WriteAsync("Hello from 2nd delegate.");
});
app.Run();
위 코드에서 app.Use
를 사용하여 요청 대리자를 연결한다.
next
매개변수는 다음 대리자이다.
next
매개변수를 호출하지 않는 경우, 이를 요청 파이프라인 단락(short-circuiting)이라고 한다.
Run
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Use(async (context, next) =>
{
// Do work that can write to the Response.
await next.Invoke();
// Do logging or other work that doesn't write to the Response.
});
app.Run(async context =>
{
await context.Response.WriteAsync("Hello from 2nd delegate.");
});
app.Run();
Run 대리자는 항상 파이프라인을 종료시킨다.
때문에 Use 메서드의 매개변수인 next
가 필요없다.
그러므로 Run 메서드가 사용된 이후의 Use나 또 다른 Run은 호출되지 않는다.
환경
.net Core에서는 앱의 실행환경을 지정할 수 있다.
실행환경의 종류는 Development
, Staging
, Production
이 있다.
아래 예시는 Development
환경에서 실행되지 않는 경우 예외 페이지를 호출하는 코드이다.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthorization();
app.MapGet("/hi", () => "Hello!");
app.MapDefaultControllerRoute();
app.MapRazorPages();
app.Run();
실행환경은 환경 변수를 사용하여 구성한다.
ASPNETCORE_ENVIRONMENT 환경 변수가 Development
인 경우, 위 코드에서 개발자 예외 페이지를 사용하게 한다.
ASPNETCORE_ENVIRONMENT 환경 변수가 Development
이외의 경우, UseExceptionHandler 메서드를 호출한다.
2️⃣
출처
https://docs.microsoft.com/ko-kr/aspnet/core/fundamentals/?view=aspnetcore-6.0&tabs=windows
https://docs.microsoft.com/ko-kr/aspnet/core/fundamentals/middleware/?view=aspnetcore-6.0