所以我有一个模型Subscription
,它继承自 Azure 的TableEntity
类,用于 WebApi Get 方法,如下所示:
[HttpGet]
public IEnumerable<Subscription> Subscribers()
在这种方法中,我Select
对我的订阅者表进行查询以查找所有订阅者,但我只想返回一些列(属性),如下所示:
var query = new TableQuery<Subscription>().Select(new string[] {
"PartitionKey",
"RowKey",
"Description",
"Verified"
});
该模型的定义如下:
public class Subscription : TableEntity
{
[Required]
[RegularExpression(@"[\w]+",
ErrorMessage = @"Only alphanumeric characters and underscore (_) are allowed.")]
[Display(Name = "Application Name")]
public string ApplicationName
{
get
{
return this.PartitionKey;
}
set
{
this.PartitionKey = value;
}
}
[Required]
[RegularExpression(@"[\w]+",
ErrorMessage = @"Only alphanumeric characters and underscore (_) are allowed.")]
[Display(Name = "Log Name")]
public string LogName
{
get
{
return this.RowKey;
}
set
{
this.RowKey = value;
}
}
[Required]
[EmailAddressAttribute]
[Display(Name = "Email Address")]
public string EmailAddress { get; set; }
public string Description { get; set; }
public string SubscriberGUID { get; set; }
public bool? Verified { get; set; }
}
以下是 API 查询的 XML 响应:
<ArrayOfSubscription>
<Subscription>
<ETag>W/"datetime'2013-03-18T08%3A54%3A32.483Z'"</ETag>
<PartitionKey>AppName1</PartitionKey><RowKey>Log1</RowKey>
<Timestamp>
<d3p1:DateTime>2013-03-18T08:54:32.483Z</d3p1:DateTime>
<d3p1:OffsetMinutes>0</d3p1:OffsetMinutes>
</Timestamp>
<ApplicationName>AppName1</ApplicationName>
<Description>Desc</Description>
<EmailAddress i:nil="true"/>
<LogName>Log1</LogName>
<SubscriberGUID i:nil="true"/>
<Verified>false</Verified>
</Subscription>
</ArrayOfSubscription>
正如你所看到的,模型不仅有一些额外的属性,比如SubscriberGUID
我不想在响应中被序列化(因为它们不在选择查询中,所以它们无论如何都是空的),而且 TableEntity 本身也有这样的字段as PartitionKey
, RowKey
, Etag
, 并且Timestamp
也被序列化了。
如何继续使用 Azure 表,但避免在响应中序列化这些我不希望用户看到的不需要的字段。