JSONスタイルの小文字の名前を持つWeb APIのアクションメソッドからオブジェクトを返す場合は、プロパティ名のエイリアスを設定してC#オブジェクト以下のJSONオブジェクトのようになります。ASP.Net Web APIの応答モデルプロパティをエイリアスする方法はありますか
C#応答モデル
public class Account
{
public int Id { get; set; }
public string AccountName { get; set; }
public decimal AccountBalance { get; set; }
}
私はあなたがこれだけJSONで動作しますJSON.NETのJsonProperty
public class Account
{
[JsonProperty(PropertyName="id")]
public int Id { get; set; }
[JsonProperty(PropertyName="account-name")]
public string AccountName { get; set; }
[JsonProperty(PropertyName="account-balance")]
public decimal AccountBalance { get; set; }
}
を使用することができます
{
"id" : 12,
"account-name" : "Primary Checking",
"account-balance" : 1000
}
、適用することに注意して ' [DataContract] 'をクラスに追加するには、直列化したい*すべてのプロパティの' [DataMember] '属性が必要です。私。既定で列挙されている10個のプロパティを持つクラスから始め、クラスに '[DataContract]'を追加し、そのプロパティに '[DataMember(Name =" a-new-name ")]'を追加すると、プロパティはもうシリアル化されません。 – AronVanAmmers
、どのようにモデル名(アカウント、ここ)を変更しますか? – sepehr