GREsau.AspNetCore
0.2.0
dotnet add package GREsau.AspNetCore --version 0.2.0
NuGet\Install-Package GREsau.AspNetCore -Version 0.2.0
<PackageReference Include="GREsau.AspNetCore" Version="0.2.0" />
paket add GREsau.AspNetCore --version 0.2.0
#r "nuget: GREsau.AspNetCore, 0.2.0"
// Install GREsau.AspNetCore as a Cake Addin
#addin nuget:?package=GREsau.AspNetCore&version=0.2.0
// Install GREsau.AspNetCore as a Cake Tool
#tool nuget:?package=GREsau.AspNetCore&version=0.2.0
JSON Property Model Metadata Names
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
).
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- No dependencies.
-
net5.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.2.0 | 387 | 4/4/2021 |