-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Closed
Labels
area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-validationIssues related to model validation in minimal and controller-based APIsIssues related to model validation in minimal and controller-based APIs
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
When an error occurs in Minimal APIs, the CustomizeProblemDetails
method is always executed, allowing customization of the ProblemDetails response. For example, this applies when an endpoint is not found or when Results.BadRequest
is returned from an endpoint. However, if validation is triggered in Minimal APIs and an error occurs, the CustomizeProblemDetails
method is not executed, and the error response structure cannot be customized.
builder.Services.AddProblemDetails(x=> x.CustomizeProblemDetails = (c) =>
{
c.ProblemDetails = new ProblemDetails(){Title = "My Error"};
});
Expected Behavior
Once validation in Minimal API fails, the CustomizeProblemDetails
method should be invoked so that we can modify the error structure like this:
builder.Services.AddProblemDetails(x=> x.CustomizeProblemDetails = (c) =>
{
if (c.HttpContext.Response.StatusCode == 400)
{
// customize the ProblemDetails for 400 Bad Request
c.ProblemDetails = new ValidationProblemDetails();
}
});
Steps To Reproduce
- Create new project (
dotnet new webapi -minimal
) with .NET SDK 10 prev5 - Add Minimal APIs validation (
builder.services.AddValidation()
) - Create new endpoint
app.MapPost("/test", (TestEntity data,) => { }
- Create entity
TestEntity
with some validation attributes - Register ProblemDetails together with
UseExceptionHandler
andUseStatusCodePages
Example:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddProblemDetails(x=> x.CustomizeProblemDetails = (c) =>
{
// it is not fired
});
builder.Services.AddValidation();
var app = builder.Build();
app.UseExceptionHandler();
app.UseStatusCodePages();
app.MapPost("/test", (TestEntity test) =>
{
return Results.Ok();
});
app.Run();
public class TestEntity
{
[Required]
public string Title { get; set; }
}
Exceptions (if any)
No response
.NET Version
10.0.100-preview.5.25277.114
Anything else?
No response
Metadata
Metadata
Assignees
Labels
area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-validationIssues related to model validation in minimal and controller-based APIsIssues related to model validation in minimal and controller-based APIs