Web api return 只有一些字段
Web api return only some fields
以下api函数returns 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",
}
是否可以return只关注?
{
"PublicId": "7eda5b39-7ef8-ea29-6136-37701e05b0cc",
"DisplayName": "Show this",
}
用jsonIgnore标记要忽略的属性:
using Newtonsoft.Json;
public class PartnerApiModel {
[JsonIgnore]
public string Name {get; set;}
}
以下api函数returns 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",
}
是否可以return只关注?
{
"PublicId": "7eda5b39-7ef8-ea29-6136-37701e05b0cc",
"DisplayName": "Show this",
}
用jsonIgnore标记要忽略的属性:
using Newtonsoft.Json;
public class PartnerApiModel {
[JsonIgnore]
public string Name {get; set;}
}