Created
May 13, 2024 05:03
-
-
Save moo2u2/9cf8cc5b3c6fb60341e6a414589ebbf0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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