I am migrating my WCF to API using .net6.0.
The WCF result is different from API result like the order of the result and the character casing of the result for the same sql query which is shown in the below example.
WCF Result
[
{
"Active" : true,
"CountryKey" : "IN",
"Id" : 1,
"Name" : "India"
},
{
"Active" : true,
"CountryKey" : "BN",
"Id" : 2,
"Name" : "Bangladesh"
}
]
API Result
[
{
"id" : 1,
"name" : "India",
"active" : true,
"countrykey" : "IN"
},
{
"id" : 2,
"name" : "Bangladesh",
"active" : true,
"countrykey" : "BN"
}
]
Here how can I retrieve the result same as WCF from the Web API .net6.0, both the order of the result and the character casing of the properties.
CodePudding user response:
You can control both of these things by altering the serialization of your objects.
Order of item rendering can be controlled using
JsonPropertyOrder
on the field and passing it an order value for each of the fields being serialized.Camel casing of the field names can be achieved by setting
PropertyNameCaseInsensitive
as an option when serializing.using System.Text.Json; using System.Text.Json.Serialization; using Microsoft.AspNetCore.Mvc; namespace Serialization.Controllers { public class CountryModel1 { public string CountryKey { get; set; } public string Name { get; set; } public int Id { get; set; } public bool Active { get; set; } }; public class CountryModel2 { [JsonPropertyOrder(2)] public string CountryKey { get; set; } [JsonPropertyOrder(4)] public string Name { get; set; } [JsonPropertyOrder(3)] public int Id { get; set; } [JsonPropertyOrder(1)] public bool Active { get; set; } }; public class HomeController : Controller { [Route("Regular")] [HttpGet] public ActionResult Regular() { return Json(_countries1); } [Route("Formatted")] [HttpGet] public ActionResult Formatted() { var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }; var person = JsonSerializer.Serialize(_countries2, options); return Content(person); } private static List<CountryModel1> _countries1 = new List<CountryModel1> { new CountryModel1() { Active = true, CountryKey = "IN", Id = 1, Name = "India" } , new CountryModel1() { Active = true, CountryKey = "BN", Id = 2, Name = "Bangladesh" } }; private static List<CountryModel2> _countries2 = new List<CountryModel2> { new CountryModel2() { Active = true, CountryKey = "IN", Id = 1, Name = "India" } , new CountryModel2() { Active = true, CountryKey = "BN", Id = 2, Name = "Bangladesh" } }; } }
Results:
/regular
[
{
"countryKey":"IN",
"name":"India",
"id":1,
"active":true
},
{
"countryKey":"BN",
"name":"Bangladesh",
"id":2,
"active":true
}
]
/formatted
[
{
"Active":true,
"CountryKey":"IN",
"Id":1,
"Name":"India"
},
{
"Active":true,
"CountryKey":"BN",
"Id":2,
"Name":"Bangladesh"
}
]
CodePudding user response:
As mentioned in the above answer, you can use JsonPropertyOrder to control the order of the output, or you can use the Order property of datamember in wcf like this:
[DataContract]
class CountryInfo
{
[DataMember(Order = 1)]
public string Id { get; set; }
[DataMember(Order = 2)]
public string Name { get; set; }
[DataMember(Order = 3)]
public string Active { get; set; }
[DataMember(Order = 4)]
public string Countrykey { get; set; }
}