public class ExceptionHandlingMiddleware { private readonly RequestDelegate _next; public ExceptionHandlingMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext httpContext) { try { await _next(httpContext); // Sonraki middleware'e devam } catch (Exception ex) { await HandleExceptionAsync(httpContext, ex); // Hata yakalandığında } } private Task HandleExceptionAsync(HttpContext context, Exception exception) { context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; // Hatanın detayları ve mesajı var result = JsonSerializer.Serialize(new { statusCode = context.Response.StatusCode, message = "An unexpected error occurred. Please try again later.", detailed = exception.Message // Detaylı mesajı geliştirme ortamında gösterebilirsin }); return context.Response.WriteAsync(result); } } app.UseMiddleware();