#define TEST_LAMBDA // MIDDLEWARE API_CONTROLLER API_CONT_SHORT DEFAULT DISABLE #if NEVER #elif MIDDLEWARE // var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddProblemDetails(); var app = builder.Build(); app.UseHttpsRedirection(); app.UseStatusCodePages(); // Middleware to handle writing problem details to the response. app.Use(async (context, next) => { await next(context); var mathErrorFeature = context.Features.Get(); if (mathErrorFeature is not null) { if (context.RequestServices.GetService() is { } problemDetailsService) { (string Detail, string Type) details = mathErrorFeature.MathError switch { MathErrorType.DivisionByZeroError => ("Divison by zero is not defined.", "https://en.wikipedia.org/wiki/Division_by_zero"), _ => ("Negative or complex numbers are not valid input.", "https://en.wikipedia.org/wiki/Square_root") }; await problemDetailsService.WriteAsync(new ProblemDetailsContext { HttpContext = context, ProblemDetails = { Title = "Bad Input", Detail = details.Detail, Type = details.Type } }); } } }); // /divide?numerator=2&denominator=4 app.MapGet("/divide", (HttpContext context, double numerator, double denominator) => { if (denominator == 0) { var errorType = new MathErrorFeature { MathError = MathErrorType.DivisionByZeroError }; context.Features.Set(errorType); return Results.BadRequest(); } return Results.Ok(numerator / denominator); }); // /squareroot?radicand=16 app.MapGet("/squareroot", (HttpContext context, double radicand) => { if (radicand < 0) { var errorType = new MathErrorFeature { MathError = MathErrorType.NegativeRadicandError }; context.Features.Set(errorType); return Results.BadRequest(); } return Results.Ok(Math.Sqrt(radicand)); }); app.MapControllers(); app.Run(); // #elif API_CONTROLLER // var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddProblemDetails(options => options.CustomizeProblemDetails = (context) => { var mathErrorFeature = context.HttpContext.Features .Get(); if (mathErrorFeature is not null) { (string Detail, string Type) details = mathErrorFeature.MathError switch { MathErrorType.DivisionByZeroError => ("Divison by zero is not defined.", "https://wikipedia.org/wiki/Division_by_zero"), _ => ("Negative or complex numbers are not valid input.", "https://wikipedia.org/wiki/Square_root") }; context.ProblemDetails.Type = details.Type; context.ProblemDetails.Title = "Bad Input"; context.ProblemDetails.Detail = details.Detail; } } ); var app = builder.Build(); app.UseHttpsRedirection(); app.UseStatusCodePages(); app.UseAuthorization(); app.MapControllers(); app.Run(); // #elif API_CONT_SHORT // var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddProblemDetails(); var app = builder.Build(); app.UseExceptionHandler(); app.UseStatusCodePages(); if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.MapControllers(); app.Run(); // #elif DEFAULT // var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); var app = builder.Build(); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run(); // #elif MIN_API // var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddProblemDetails(options => options.CustomizeProblemDetails = (context) => { var mathErrorFeature = context.HttpContext.Features .Get(); if (mathErrorFeature is not null) { (string Detail, string Type) details = mathErrorFeature.MathError switch { MathErrorType.DivisionByZeroError => ("Divison by zero is not defined.", "https://wikipedia.org/wiki/Division_by_zero"), _ => ("Negative or complex numbers are not valid input.", "https://wikipedia.org/wiki/Square_root") }; context.ProblemDetails.Type = details.Type; context.ProblemDetails.Title = "Bad Input"; context.ProblemDetails.Detail = details.Detail; } } ); var app = builder.Build(); app.UseHttpsRedirection(); app.UseStatusCodePages(); app.UseAuthorization(); app.MapControllers(); // /divide?numerator=2&denominator=4 app.MapGet("/divide", (HttpContext context, double numerator, double denominator) => { if (denominator == 0) { var errorType = new MathErrorFeature { MathError = MathErrorType.DivisionByZeroError }; context.Features.Set(errorType); return Results.BadRequest(); } var calculation = numerator / denominator; return Results.Ok(calculation); }); // /squareroot?radicand=16 app.MapGet("/squareroot", (HttpContext context, double radicand) => { if (radicand < 0) { var errorType = new MathErrorFeature { MathError = MathErrorType.NegativeRadicandError }; context.Features.Set(errorType); return Results.BadRequest(); } return Results.Ok(Math.Sqrt(radicand)); }); app.Run(); // #elif DISABLE // var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers() .ConfigureApiBehaviorOptions(options => { options.SuppressMapClientErrors = true; }); var app = builder.Build(); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run(); // #elif TEST_LAMBDA // using Microsoft.AspNetCore.Diagnostics; using static System.Net.Mime.MediaTypeNames; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddProblemDetails(); var app = builder.Build(); app.UseExceptionHandler(); app.UseStatusCodePages(); if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(exceptionHandlerApp => { exceptionHandlerApp.Run(async context => { context.Response.StatusCode = StatusCodes.Status500InternalServerError; context.Response.ContentType = Text.Plain; var title = "Bad Input"; var detail = "Invalid input"; var type = "https://errors.example.com/badInput"; if (context.RequestServices.GetService() is { } problemDetailsService) { var exceptionHandlerFeature = context.Features.Get(); var exceptionType = exceptionHandlerFeature?.Error; if (exceptionType != null && exceptionType.Message.Contains("infinity")) { title = "Arguement exception"; detail = "Invalid input"; type = "https://errors.example.com/arguementException"; } await problemDetailsService.WriteAsync(new ProblemDetailsContext { HttpContext = context, ProblemDetails = { Title = title, Detail = detail, Type = type } }); } }); }); } app.MapControllers(); app.Run(); // #endif