Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>ProblemDetailsServiceSample</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-preview.7.22376.6" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

</Project>
85 changes: 85 additions & 0 deletions fundamentals/middleware/problem-details-service/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Microsoft.AspNetCore.Http.Features;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
Comment thread
sammychinedu2ky marked this conversation as resolved.
builder.Services.AddProblemDetails(options =>
options.CustomizeProblemDetails = (context) =>
{
var mathErrorFeature = context.HttpContext.Features.GetRequiredFeature<MathErrorFeature>();
(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")
};

context.ProblemDetails.Type = details.Type;
context.ProblemDetails.Title = "Wrong Input";
context.ProblemDetails.Detail = details.Detail;
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
Comment thread
sammychinedu2ky marked this conversation as resolved.

app.UseHttpsRedirection();

// middleware to handle writing problem details to the response
app.Use(async (context, next) =>
{
// added the MathErrorFeature to the request pipeline
var mathErrorFeature = new MathErrorFeature();
context.Features.Set(mathErrorFeature);
await next(context);
});

app.UseStatusCodePages();

// endpoint for dividing numbers
app.MapGet("/divide", (HttpContext context, double numerator, double denominator) =>
{
if (denominator == 0)
{
context.Features.GetRequiredFeature<MathErrorFeature>().MathError = MathErrorType.DivisionByZeroError;
return Results.BadRequest();
}

var calculation = numerator / denominator;
return Results.Ok(calculation);
});

// endpoint for obtaining the squareroot of a number
app.MapGet("/squareroot", (HttpContext context, int radicand) =>
{
if (radicand < 0)
{
context.Features.GetRequiredFeature<MathErrorFeature>().MathError = MathErrorType.NegativeRadicandError;
return Results.BadRequest();
}

var calculation = Math.Sqrt(radicand);
return Results.Ok(calculation);
});

app.Run();

// Custom math errors
enum MathErrorType
{
DivisionByZeroError,
NegativeRadicandError
}

// Custom Http Request Feature
class MathErrorFeature
{
public MathErrorType MathError { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}