c# - Web api 只返回一些字段
问题描述
以下 api 函数返回 PartnerApiModel 对象,这很好。
[HttpGet]
public HttpResponseMessage Get()
{
PartnerAPIModel apiPartner = new PartnerAPIModel();
apiPartner.PublicId = "1";
apiPartner.DisplayName = "Show this";
apiPartner.Name = "Test";
return Request.CreateResponse(HttpStatusCode.OK, apiPartner);
}
结果:
{
"PublicId": "7eda5b39-7ef8-ea29-6136-37701e05b0cc",
"DisplayName": "Show this",
"Name": "Test",
}
是否可以仅返回以下内容?
{
"PublicId": "7eda5b39-7ef8-ea29-6136-37701e05b0cc",
"DisplayName": "Show this",
}
解决方案
用 jsonIgnore 标记要忽略的属性:
using Newtonsoft.Json;
public class PartnerApiModel {
[JsonIgnore]
public string Name {get; set;}
}
推荐阅读
- android - 如何将自定义菜单添加到底部应用栏?
- java - 我可以在不安装的情况下从 Java 代码调用 Bazel 吗?
- c# - 编辑gridview上的滚动条移动并且不保持其位置
- python-3.x - 更改 seaborn pair plot 图形大小
- python - 从另一个目录中查找数据 (.txt) 并将名称保存在列表中
- java - 当我尝试访问手机的外部存储器时应用程序崩溃
- javascript - 如何在 React/Express 应用程序上正确设置 Socket.io?
- sql - 如何优化以下 Oracle SQL 查询
- excel - 查找范围内最后一个空白单元格的公式
- android - 将从 api 获得的令牌保存到内部存储