Skip to content

Instantly share code, notes, and snippets.

@moo2u2
Created May 13, 2024 05:03
Show Gist options
  • Save moo2u2/9cf8cc5b3c6fb60341e6a414589ebbf0 to your computer and use it in GitHub Desktop.
Save moo2u2/9cf8cc5b3c6fb60341e6a414589ebbf0 to your computer and use it in GitHub Desktop.
builder.Services.AddControllers().AddJsonOptions(options =>
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())
).ConfigureApiBehaviorOptions(option =>
{
option.InvalidModelStateResponseFactory = context =>
{
string errorInfo = string.Empty;
try
{
if (context.ModelState.Count > 0 && context.ModelState.First().Value != null && context.ModelState.First().Value?.Errors.Count > 0)
{
errorInfo = context.ModelState.First().Value?.Errors.First().ErrorMessage ?? string.Empty;
}
}
catch (Exception ex)
{
errorInfo = $"Issue reading errorInfo {ex}";
}
Console.Error.WriteLine(errorInfo);
return new BadRequestObjectResult(context.ModelState);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment