I have created a register user endpoint in an ASP.NET Core app. For that I am using the following DTO:
namespace CommandAPI.Models
{
using System.ComponentModel.DataAnnotations;
public class RegisterUserDto
{
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string Password { get; set; }
}
}
If I make a https call to this endpoint with following body :
[
{
"Email" : "abc@email.com",
"Password" : "abc@123#"
}
]
The endpoint returns the following error response :
{
"errors": {
"": [
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'CommandAPI.Models.RegisterUserDto' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath '', line 1, position 1."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|3dcc7b94-4f57775eed318939."
}
Though the error is correct, but it contains my dto class name with namespace. How to suppress this detail in response?
I am using the following service to convert incoming json request to C# object :
services.AddControllers().AddNewtonsoftJson(s => s.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver());