[C#/ASP.NET MVC/.NETCORE] UseMiddlewareExtensions 클래스 : UseMiddleware 확장 메소드를 사용해 컨텐트 생성 미들웨어 등록하기
C#/ASP.NET MVC 2020. 11. 16. 17:33728x90
728x170
▶ ContentMiddleware.cs
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
namespace TestProject
{
/// <summary>
/// 컨텐트 생성 미들웨어
/// </summary>
public class ContentMiddleware
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 다음 미들웨어
/// </summary>
private RequestDelegate next;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - ContentMiddleware(next)
/// <summary>
/// 생성자
/// </summary>
/// <param name="next">다음 미들웨어</param>
public ContentMiddleware(RequestDelegate next)
{
this.next = next;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 호출하기 - Invoke(context)
/// <summary>
/// 호출하기
/// </summary>
/// <param name="context">HTTP 컨텍스트</param>
/// <returns>태스크</returns>
public async Task Invoke(HttpContext context)
{
if(context.Request.Path.ToString().ToLower() == "/hello")
{
await context.Response.WriteAsync("<h1>Hello from ContentMiddleware.</h1>");
}
else
{
await next.Invoke(context);
}
}
#endregion
}
}
728x90
▶ Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
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();
}
#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.UseMiddleware<ContentMiddleware>();
app.UseEndpoints
(
endpoints =>
{
endpoints.MapControllerRoute
(
name : "default",
pattern : "{controller=Home}/{action=Index}/{id?}"
);
}
);
}
#endregion
}
}
728x90
그리드형(광고전용)