I need to receive a data and convert it to Json changing the name of a property:
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);
}
I tried to use JsonProperty like this:
public class MyClass {
[JsonProperty(PropertyName = "Y")]
public string X { get; set;}
}
But this does not work because when binding the property on action, it looks for a 'Y' and not for my 'X', so the property 'X' comes null;
So how can I just ignore model binding and converts my 'X' to 'Y' or how can I change the property name when serializing?.