https://github.com/mgernand/aspnetcore.problemdetails
A middleware to create RFC 7807 problem details for APIs.
https://github.com/mgernand/aspnetcore.problemdetails
api asp-net-core aspnet-core aspnetcore dotnet dotnet-core dotnetcore middleware problem-details
Last synced: 5 months ago
JSON representation
A middleware to create RFC 7807 problem details for APIs.
- Host: GitHub
- URL: https://github.com/mgernand/aspnetcore.problemdetails
- Owner: mgernand
- License: mit
- Created: 2022-05-06T11:37:50.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-14T12:36:26.000Z (6 months ago)
- Last Synced: 2024-11-14T12:59:33.401Z (6 months ago)
- Topics: api, asp-net-core, aspnet-core, aspnetcore, dotnet, dotnet-core, dotnetcore, middleware, problem-details
- Language: C#
- Homepage:
- Size: 77.1 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AspNetCore.ProblemDetails
A middleware to create RFC 7807 problem details for APIs.
## Usage
To configute the mandatory services for the problem details middleware just execute
```AddProblemDetails``` on the ```IMvcBuilder``` instance in your startup code.```C#
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);// Add services to the container.
builder.Services
.AddControllers()
.AddProblemDetails(options =>
{
// Only include exception details in a development environment. This is the default
// behavior and is included to demo purposes.
options.IncludeExceptionDetails = (context, exception) => builder.Environment.IsDevelopment();// Use the status code 501 for this type of exception.
options.MapStatusCode(HttpStatusCode.NotImplemented);// Use the status code 501 for this type of exception.
options.MapStatusCode(HttpStatusCode.InternalServerError);// Add a fallback for all other exceptions with the status code 500.
options.MapStatusCode(HttpStatusCode.InternalServerError);
});
```After you've built your web application instance add the middlware to the pipeline by
calling ```UseProblemDetails```.```C#
WebApplication app = builder.Build();// Configure the HTTP request pipeline.
app.UseCors();
app.UseProblemDetails();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
```