AspNetCore.Docs.Samples/fundamentals/middleware/problem-details-service/Program.cs

222 lines
6.2 KiB
C#
Raw Normal View History

2022-09-20 08:32:07 +08:00
#define API_CONT_SHORT // FIRST MIDDLEWARE API_CONTROLLER API_CONT_SHORT
2022-09-09 02:04:44 +08:00
#if NEVER
#elif FIRST
// <snippet_1>
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddProblemDetails(options =>
options.CustomizeProblemDetails = (context) =>
{
2022-09-09 02:04:44 +08:00
var mathErrorFeature = context.HttpContext.Features
.Get<MathErrorFeature>();
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 = "Wrong Input";
context.ProblemDetails.Detail = details.Detail;
}
}
);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseStatusCodePages();
2022-09-09 02:04:44 +08:00
// /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);
});
2022-09-09 02:04:44 +08:00
// /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();
2022-09-09 02:04:44 +08:00
// </snippet_1>
#elif MIDDLEWARE
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddProblemDetails();
2022-09-09 02:04:44 +08:00
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
2022-09-09 02:04:44 +08:00
app.UseSwagger();
app.UseSwaggerUI();
}
2022-09-09 02:04:44 +08:00
app.UseHttpsRedirection();
app.UseStatusCodePages();
2022-09-20 08:05:18 +08:00
// Middleware to handle writing problem details to the response.
2022-09-09 02:04:44 +08:00
app.Use(async (context, next) =>
{
2022-09-09 02:04:44 +08:00
await next(context);
var mathErrorFeature = context.Features.Get<MathErrorFeature>();
if (mathErrorFeature is not null)
{
if (context.RequestServices.GetService<IProblemDetailsService>() is
{ } problemDetailsService)
{
(string Detail, string Type) details = mathErrorFeature.MathError switch
{
MathErrorType.DivisionByZeroError => ("The number you inputed is zero",
"https://en.wikipedia.org/wiki/Division_by_zero"),
_ => ("Negative or complex numbers are not handled",
"https://en.wikipedia.org/wiki/Square_root")
};
await problemDetailsService.WriteAsync(new ProblemDetailsContext
{
HttpContext = context,
ProblemDetails =
{
Title = "Wrong Input",
Detail = details.Detail,
Type = details.Type
}
});
}
}
2022-09-09 02:04:44 +08:00
});
// /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);
2022-09-09 02:04:44 +08:00
return Results.BadRequest();
}
return Results.Ok(numerator / denominator);
2022-09-09 02:04:44 +08:00
});
// /squareroot?radicand=16
app.MapGet("/squareroot", (HttpContext context, double radicand) =>
2022-09-09 02:04:44 +08:00
{
if (radicand < 0)
{
var errorType = new MathErrorFeature { MathError = MathErrorType.NegativeRadicandError };
context.Features.Set(errorType);
2022-09-09 02:04:44 +08:00
return Results.BadRequest();
}
return Results.Ok(Math.Sqrt(radicand));
2022-09-09 02:04:44 +08:00
});
app.Run();
#elif API_CONTROLLER
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddProblemDetails(options =>
options.CustomizeProblemDetails = (context) =>
{
var mathErrorFeature = context.HttpContext.Features
.Get<MathErrorFeature>();
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 = "Wrong Input";
context.ProblemDetails.Detail = details.Detail;
}
}
);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseStatusCodePages();
app.UseAuthorization();
app.MapControllers();
app.Run();
2022-09-20 08:32:07 +08:00
#elif API_CONT_SHORT
// <snippet_apishort>
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddProblemDetails();
var app = builder.Build();
app.UseExceptionHandler();
app.UseStatusCodePages();
2022-09-20 08:32:07 +08:00
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.MapControllers();
app.Run();
// </snippet_apishort>
2022-09-09 02:04:44 +08:00
#endif