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>
|
2022-09-02 09:57:59 +08:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
builder.Services.AddProblemDetails(options =>
|
|
|
|
options.CustomizeProblemDetails = (context) =>
|
|
|
|
{
|
2022-09-09 14:14:44 +08:00
|
|
|
|
2022-09-09 02:04:44 +08:00
|
|
|
var mathErrorFeature = context.HttpContext.Features
|
2022-09-15 10:06:37 +08:00
|
|
|
.Get<MathErrorFeature>();
|
|
|
|
if (mathErrorFeature is not null)
|
2022-09-02 09:57:59 +08:00
|
|
|
{
|
2022-09-09 14:14:44 +08:00
|
|
|
(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-09-02 09:57:59 +08:00
|
|
|
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
|
2022-09-02 09:57:59 +08:00
|
|
|
app.MapGet("/divide", (HttpContext context, double numerator, double denominator) =>
|
|
|
|
{
|
|
|
|
if (denominator == 0)
|
|
|
|
{
|
2022-09-15 10:06:37 +08:00
|
|
|
var errorType = new MathErrorFeature { MathError = MathErrorType.DivisionByZeroError };
|
|
|
|
context.Features.Set(errorType);
|
2022-09-02 09:57:59 +08:00
|
|
|
return Results.BadRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
var calculation = numerator / denominator;
|
|
|
|
return Results.Ok(calculation);
|
|
|
|
});
|
|
|
|
|
2022-09-09 02:04:44 +08:00
|
|
|
// /squareroot?radicand=16
|
2022-09-10 05:11:17 +08:00
|
|
|
app.MapGet("/squareroot", (HttpContext context, double radicand) =>
|
2022-09-02 09:57:59 +08:00
|
|
|
{
|
|
|
|
if (radicand < 0)
|
|
|
|
{
|
2022-09-15 10:06:37 +08:00
|
|
|
var errorType = new MathErrorFeature { MathError = MathErrorType.NegativeRadicandError };
|
|
|
|
context.Features.Set(errorType);
|
2022-09-02 09:57:59 +08:00
|
|
|
return Results.BadRequest();
|
|
|
|
}
|
|
|
|
|
2022-09-10 05:11:17 +08:00
|
|
|
return Results.Ok(Math.Sqrt(radicand));
|
2022-09-02 09:57:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
app.Run();
|
2022-09-09 14:14:44 +08:00
|
|
|
|
2022-09-09 02:04:44 +08:00
|
|
|
// </snippet_1>
|
|
|
|
#elif MIDDLEWARE
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
builder.Services.AddSwaggerGen();
|
2022-09-09 11:47:17 +08:00
|
|
|
builder.Services.AddProblemDetails();
|
2022-09-09 02:04:44 +08:00
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
2022-09-02 09:57:59 +08:00
|
|
|
{
|
2022-09-09 02:04:44 +08:00
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
2022-09-02 09:57:59 +08:00
|
|
|
}
|
|
|
|
|
2022-09-09 02:04:44 +08:00
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
2022-09-09 14:14:44 +08:00
|
|
|
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-02 09:57:59 +08:00
|
|
|
{
|
2022-09-09 02:04:44 +08:00
|
|
|
await next(context);
|
2022-09-15 10:06:37 +08:00
|
|
|
var mathErrorFeature = context.Features.Get<MathErrorFeature>();
|
|
|
|
if (mathErrorFeature is not null)
|
2022-09-09 11:47:17 +08:00
|
|
|
{
|
2022-09-10 05:11:17 +08:00
|
|
|
if (context.RequestServices.GetService<IProblemDetailsService>() is
|
|
|
|
{ } problemDetailsService)
|
2022-09-09 11:47:17 +08:00
|
|
|
{
|
|
|
|
(string Detail, string Type) details = mathErrorFeature.MathError switch
|
|
|
|
{
|
2022-09-09 14:14:44 +08:00
|
|
|
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")
|
2022-09-09 11:47:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-09-15 10:06:37 +08:00
|
|
|
var errorType = new MathErrorFeature { MathError = MathErrorType.DivisionByZeroError };
|
|
|
|
context.Features.Set(errorType);
|
2022-09-09 02:04:44 +08:00
|
|
|
return Results.BadRequest();
|
|
|
|
}
|
|
|
|
|
2022-09-10 05:11:17 +08:00
|
|
|
return Results.Ok(numerator / denominator);
|
2022-09-09 02:04:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// /squareroot?radicand=16
|
2022-09-10 05:11:17 +08:00
|
|
|
app.MapGet("/squareroot", (HttpContext context, double radicand) =>
|
2022-09-09 02:04:44 +08:00
|
|
|
{
|
|
|
|
if (radicand < 0)
|
|
|
|
{
|
2022-09-15 10:06:37 +08:00
|
|
|
var errorType = new MathErrorFeature { MathError = MathErrorType.NegativeRadicandError };
|
|
|
|
context.Features.Set(errorType);
|
2022-09-09 02:04:44 +08:00
|
|
|
return Results.BadRequest();
|
|
|
|
}
|
|
|
|
|
2022-09-10 05:11:17 +08:00
|
|
|
return Results.Ok(Math.Sqrt(radicand));
|
2022-09-09 02:04:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
app.Run();
|
2022-09-09 14:14:44 +08:00
|
|
|
|
2022-09-10 05:11:17 +08:00
|
|
|
#elif API_CONTROLLER
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
|
|
|
|
builder.Services.AddProblemDetails(options =>
|
2022-09-15 10:06:37 +08:00
|
|
|
options.CustomizeProblemDetails = (context) =>
|
2022-09-10 05:11:17 +08:00
|
|
|
{
|
|
|
|
|
2022-09-15 10:06:37 +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;
|
|
|
|
}
|
2022-09-10 05:11:17 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
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-10 05:11:17 +08:00
|
|
|
|
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
|