Utilities for ASP.NET Core web applications.
Install via NuGet:
> dotnet add package GREsau.AspNetCore
Basic usage:
services.AddControllers().AddJsonPropertyModelMetadataNames();
By default, when model validation fails, MVC will use the C# property names in the resulting ValidationProblemDetails
. This will typically returned to the client as a JSON body of a 400 Bad Request response.
For example, given the model class:
public class MyModel
{
[Range(1, 100)]
[JsonPropertyName("foo")]
public int Number { get; set; }
[Required]
public string Text { get; set; }
}
If the client POSTs the JSON { "foo": 0 }
to a controller that takes MyModel
, then they will receive a 400 with a body similar to:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|01234567-0123456789abcdef.",
"errors": {
"Number": [
"The field Number must be between 1 and 100."
],
"Text": [
"The field Text is required."
]
}
}
...even though the client would have no prior knowledge of the identifier Number
. To make MVC use the identifier foo
instead, you can use this library's AddJsonPropertyModelMetadataNames()
extension method on IMvcBuilder
(or IMvcCoreBuilder
), e.g. in your Startup class:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddJsonPropertyModelMetadataNames();
}
Then, the response body would be:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|01234567-0123456789abcdef.",
"errors": {
"foo": [
"The field foo must be between 1 and 100."
],
"text": [
"The field text is required."
]
}
}
Be aware this will alter the name used for model binding of any properties, unless they are explicitly overridden (e.g. using a [BindProperty(Name = "...")]
attribute). However, it will not alter the model binding name for any method parameters, or properties of a controller (any subclass of ControllerBase
).