-
Notifications
You must be signed in to change notification settings - Fork 711
Consider to return camelCase error messages #195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I suppose we can override default behavior: services.AddApiVersioning(o =>
{
o.ErrorResponses = //create a class implements IErrorResponseProvider
}); |
Thanks for bringing this to my attention. I didn't realize that the default JSON settings in ASP.NET Core had changed. In ASP.NET Web API, the error messages are based on the HttpError class. Ultimately, this class was just a basic extension of Dictionary<string, object>. As a result, the ASP.NET Core DefaultErrorResponseProvider uses a simple dictionary to generate the error response content. Unfortunately, the new, default JSON settings in ASP.NET Core does not apply to dictionaries (intentionally). There are number of issues and discussions in the ASP.NET repo about this. To achieve your desired result, you just need to change your setup like this: services.AddMvc()
.AddJsonOptions( o =>
o.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver() ); This should work as is today. The error responses, however, are still inconsistent with the default JSON serialization behavior so it's worth changing the way responses are generated. I'll update the implementation to return an object instead of a dictionary to provide the content. This will achieve parity with the default serialization behavior. One final note. The enhancement to align serialization behavior will result in one small side effect. Null values are returned in JSON with the default settings. To suppress this, you'll still need to change the default JSON settings, but it will apply to the entire ASP.NET Core application. services.AddMvc()
.AddJsonOptions( o => o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore ); I find this to be a negligible compromise. |
That would be great. Thanks. |
…ich will result in matching default the JSON serialization settings. Resolves #195
…ich will result in matching default the JSON serialization settings. Resolves #195
Uh oh!
There was an error while loading. Please reload this page.
Instead of
return this:
Because it's more natural for javascript clients and ASP.NET Core team also use camelCase.
Or is there any configuration?
The text was updated successfully, but these errors were encountered: