A set of technologies in .NET for building web applications and web services. Miscellaneous topics that do not fit into specific categories.
Hi @Cenk,
I just want to display errors not the type, title traceId.
I have a work demo like below, you can refer to it.
- Create a static class with method like:
public static class InvalidModelStateResponse
{
public static IActionResult MakeValidationResponse(ActionContext context)
{
var Details = new ValidationProblemDetails(context.ModelState)
{
Status = StatusCodes.Status400BadRequest,
};
var errorDetails = new ErrorDetails
{
Status = Details.Status,
Errors = Details.Errors,
};
var result = new BadRequestObjectResult(errorDetails);
result.ContentTypes.Add("application/json");
return result;
}
}
2.Create a class for the response:
public class ErrorDetails
{
public int? Status { get; set; }
public IDictionary<string, string[]> Errors { get; set; }
}
3.Add the following code in the Program.cs:
builder.Services.AddControllers().ConfigureApiBehaviorOptions(options =>
{options.InvalidModelStateResponseFactory = InvalidModelStateResponse.MakeValidationResponse;});
4.result:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Qing Guo