MVC4 が NewtonSoft Json デシリアライゼーションを使用していることは知っています。JsonIgnore/DataMemberIngore などのデータ注釈を使用せずに、クライアントへのシリアル化でプロパティを除外する方法を考えていました (アセンブリは他の場所で使用され、変更できません。カスタム フォーマッタ/JsonSerializerSettings/Dynamic ContractResolver を実装できますか?などを特定のオブジェクト タイプに適用してから、特定のプロパティ名を除外しますか?
どんな助けでも大歓迎です。
編集。最初の試みとして次のことを思いつきました。誰かがよりエレガントなソリューションを持っている場合は、私に知らせてください...
public class DynamicContractResolver : DefaultContractResolver
{
public DynamicContractResolver()
{
}
protected override IList<JsonProperty> CreateProperties(Type type, Newtonsoft.Json.MemberSerialization memberSerialization)
{
IList<JsonProperty> properties = base.CreateProperties(type, Newtonsoft.Json.MemberSerialization.Fields);
if (type == typeof(SomeType))
{
var matchedProp = properties.Where(v=> v.PropertyName=="SomeProperty").FirstOrDefault();
if (matchedProp!=null)
{
properties.Remove(matchedProp);
}
}
return properties;
}
}
global.asax に組み込まれています。
HttpConfiguration config = GlobalConfiguration.Configuration;
JsonSerializerSettings serializerSetting = new JsonSerializerSettings
{
ContractResolver = new DynamicContractResolver(),
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
};
config.Formatters.JsonFormatter.SerializerSettings = serializerSetting;
よろしくフィル