[C#/ASP.NET MVC/.NETCORE] IExceptionHandlerPathFeature 인터페이스 : 오류 처리기 컨트롤러 또는 페이지에서 예외 및 요청 경로 구하기
C#/ASP.NET MVC 2020. 10. 31. 21:03728x90
반응형
728x170
▶ Properties/launchSettings.json
{
"iisSettings" :
{
"windowsAuthentication" : false,
"anonymousAuthentication" : true,
"iisExpress" :
{
"applicationUrl" : "http://localhost:11944",
"sslPort" : 44399
}
},
"profiles" :
{
"IIS Express" :
{
"commandName" : "IISExpress",
"launchBrowser" : true,
"environmentVariables" :
{
"ASPNETCORE_ENVIRONMENT" : "Production"
}
},
"TestProject" :
{
"commandName" : "Project",
"launchBrowser" : true,
"applicationUrl" : "https://localhost:5001;http://localhost:5000",
"environmentVariables" :
{
"ASPNETCORE_ENVIRONMENT" : "Production"
}
}
}
}
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.AddRazorPages();
}
#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("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints
(
endpoints =>
{
endpoints.MapRazorPages();
}
);
}
#endregion
}
}
▶ Pages/Index.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
namespace TestProject.Pages
{
/// <summary>
/// 인덱스 모델
/// </summary>
public class IndexModel : PageModel
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region GET 요청시 처리하기 - OnGet()
/// <summary>
/// GET 요청시 처리하기
/// </summary>
public void OnGet()
{
throw new Exception();
}
#endregion
}
}
▶ Pages/Error.cshtml.cs
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace TestProject.Pages
{
/// <summary>
/// 에러 모델
/// </summary>
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public class ErrorModel : PageModel
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 요청 ID - RequestID
/// <summary>
/// 요청 ID
/// </summary>
public string RequestID { get; set; }
#endregion
#region 요청 ID 표시 여부 - ShowRequestID
/// <summary>
/// 요청 ID 표시 여부
/// </summary>
public bool ShowRequestID => !string.IsNullOrEmpty(RequestID);
#endregion
#region 예외 메시지 - ExceptionMessage
/// <summary>
/// 예외 메시지
/// </summary>
public string ExceptionMessage { get; set; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - ErrorModel()
/// <summary>
/// 생성자
/// </summary>
public ErrorModel()
{
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region GET 요청시 처리하기 - OnGet()
/// <summary>
/// GET 요청시 처리하기
/// </summary>
public void OnGet()
{
RequestID = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
IExceptionHandlerPathFeature feature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
StringBuilder stringBuilder = new StringBuilder();
if(feature?.Error is FileNotFoundException)
{
stringBuilder.Append("File error thrown");
}
else
{
stringBuilder.Append("Error thrown");
}
if(feature?.Path == "/" || feature?.Path == "/index")
{
stringBuilder.Append(" from home page");
}
else
{
stringBuilder.Append($" from {feature?.Path}");
}
ExceptionMessage = stringBuilder.ToString();
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
댓글을 달아 주세요