Say I have an async web api 2 controller method returning a Created ActionResult like:
return Created(new Uri(Request.RequestUri + "/" + Item.Id), Item);
Item has a property ItemDescriptions which I would like to truncate on response. How do I define serialization for a Created Web Api response like this?
I tried creating a custom resolver like:
public class ItemContractResolver: CamelCasePropertyNamesContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (property.PropertyName == "ItemDescriptions")
{
property.ShouldSerialize = i => false;
property.ShouldDeserialize = i => false;
}
return property;
}
}
Which I later set in the Controller constructor like:
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver = new ItemContractResolver();
But that sadly does not work and the ItemDescriptions property is still serialized.