的ASP.NET Web API部分缓解JSON序列化 [英] ASP.NET Web API partial response Json serialization

查看:130
本文介绍了的ASP.NET Web API部分缓解JSON序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了支持部分响应的Web API。

I am implementing a Web API that supports partial response.

/api/users?fields=id,name,age

由于该类用户

[JsonObject(MemberSerialization.OptIn)]
public partial class User
{
  [JsonProperty]
  public int id { get; set; }

  [JsonProperty]
  public string firstname { get; set; }

  [JsonProperty]
  public string lastname { get; set; }

  [JsonProperty]
  public string name { get { return firstname + " " + lastname; } }

  [JsonProperty]
  public int age { get; set; }
}

序列化的所有属性时,

将JSON格式的伟大工程,但我不能设法修改它在运行时告诉它忽略某些属性,根据查询参数的字段。

The Json formatter works great when serializing the all properties, but I can't manage to modify it at runtime to tell it to ignore some of the properties, depending on the query parameter "fields".

我与JsonMediaTypeFormatter工作。

I am working with JsonMediaTypeFormatter.

我按照的http://的toString。它/ 2012/07/18 /定制-JSON的结果 - 在网络的API / 以定制格式化,但我不能找到如何强制格式化忽略某些属性的例子。

I have followed http://tostring.it/2012/07/18/customize-json-result-in-web-api/ in order to customize the formatter, but I can't find any example on how to force the formatter to ignore some properties.

推荐答案

创建自己的<一个href=\"http://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_Serialization_IContractResolver.htm\"相对=nofollow> IContractResolver 告诉JSON.NET哪些属性需要被序列化。有官方文件在例如,你可以采取汲取灵感从

这篇关于的ASP.NET Web API部分缓解JSON序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆