For using Patch in my Blazor application, we have used builder.Services.AddControllers().AddNewtonsoftJson()
. But after adding it everything works fine if we dont return valdiationerror into response. When we return validation response, it throws an error as below,
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Unable to find a constructor to use for type System.ComponentModel.DataAnnotations.ValidationResult. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'validationResult.validationResults[0].memberNames', line 1, position 493.
Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type System.ComponentModel.DataAnnotations.ValidationResult. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'validationResult.validationResults[0].memberNames', line 1, position 493.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
This is what I have done for grabbing response(please ignore the error at Mymodel, I changed it for screenshot only). It breaks at DeserializeObject only when res has validationresults.
response = await Http.PostAsJsonAsync(Constants.uri, editModel);
var res = response.Content.ReadAsStringAsync().Result;
var validationResponse = JsonConvert.DeserializeObject<Mymodel>(res);
And when I we will remove .AddNewtonsoftJson(), it returns proper validation message but Patch wont work!
Mymodel looks like this:
public class MymodelResponse : BaseResponse
{
public int MymodelId { get; set; }
public string? Phone { get; set; }
public long CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public long? ModifiedBy { get; set; }
public DateTime? ModifiedDate { get; set; }
public bool IsDeleted { get; set; }
public string? Notes { get; set; }
public MymodelResponse(List<ValidationResult> validationResults) : base(validationResults)
{
}
}
And BaseResponse
has a property
public List<ValidationResult> ValidationResults { get; set; }
Which is set in the constructor.