If you want to return objects from action methods in Web Api with JSON style lowercase names, is there a way to alias the property names so that the C# object below looks like the JSON object that follows.
C# Response Model
public class Account
{
public int Id { get; set; }
public string AccountName { get; set; }
public decimal AccountBalance { get; set; }
}
JSON that I’d like to be returned
{
"id" : 12,
"account-name" : "Primary Checking",
"account-balance" : 1000
}
You can use JSON.NET’s
JsonPropertyThis will only work with JSON.NET – obviously. If you want to be more agnostic, and have this type of naming to be able to other potential formatters (i.e. you’d change JSON.NET to something else, or for XML serialization), reference
System.Runtime.Serializationand use: