C# 使用JsonProperty时忽略模型绑定
我需要接收数据并将其转换为Json更改属性名称:C# 使用JsonProperty时忽略模型绑定,c#,json.net,C#,Json.net,我需要接收数据并将其转换为Json更改属性名称: public class MyClass { public string X { get; set;} } public IHttpActionResult Method(MyClass model) { //here my property 'X' needs to be converted into 'Y' var json = JsonConvert.SerializeObject(model); re
public class MyClass {
public string X { get; set;}
}
public IHttpActionResult Method(MyClass model)
{
//here my property 'X' needs to be converted into 'Y'
var json = JsonConvert.SerializeObject(model);
return OK(json);
}
我尝试像这样使用JsonProperty:
public class MyClass {
[JsonProperty(PropertyName = "Y")]
public string X { get; set;}
}
但这不起作用,因为在绑定操作上的属性时,它查找的是“Y”,而不是我的“X”,因此属性“X”为空
那么,我如何忽略模型绑定并将我的“X”转换为“Y”,或者在序列化时如何更改属性名称?您可以向我们展示您的绑定吗?重命名应该是可能的:但我不知道你对其余部分的意思。试试看