c# - 防止 Azure TableEntity 属性在 MVC 4 WebAPI 中被序列化

标签 c# serialization asp.net-mvc-4 asp.net-web-api azure-table-storage

所以我有一个模型 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,我不想在响应中对其进行序列化(因为它们不在选择查询中,所以它们无论如何都是 null),但是 TableEntity 本身有 PartitionKeyRowKeyEtagTimestamp 等字段,它们是也正在连载中。

如何继续使用 Azure 表,但避免在响应中序列化这些我不希望用户看到的不需要的字段。

最佳答案

不同意使用特定 DTO 的答案,但 Microsoft.WindowsAzure.Storage 程序集现在提供了一个属性,IgnorePropertyAttribute ,您可以用它装饰您的公共(public)属性(property)以避免序列化。

我还没有真正尝试过,但是 TableEntity 上有一个方法称为 ShouldSkipProperty()在返回之前会检查很多东西 false (即不要跳过):

  • 属性名称是“PartitionKey”、“RowKey”、“Timestamp”或“ETag”之一 -> 跳过
  • getter 和 setter 中的任何一个都是非公开的 -> 跳过
  • 是否是静态的 -> 跳过
  • 该属性是否具有属性 IgnorePropertyAttribute -> 跳过

看起来它会成功。

关于c# - 防止 Azure TableEntity 属性在 MVC 4 WebAPI 中被序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15489232/

相关文章:

c# - 除了方法在 linq 中如何工作

c# - 将 InputStream 和 OutputStream 转换为 System.IO.Stream - Xamarin.iOS (MonoTouch)

c# - 使用无参数构造函数初始化 Struct

c# - 列表<对象> 与列表<动态>

c# - 为什么二进制序列化比 xml 序列化更快?

java - 在 avro 模式中使用 "default"

c# - 在不实现任何自定义序列化/反序列化时是否需要实现 ISerializable 接口(interface)

asp.net-mvc - 在 MVC 4 应用程序中处理带有自定义错误的 UnauthorizedAccessException

c# - HttpWebRequest 可能会减慢网站速度

c# - 预定义类型 'System.Threading.Tasks.Task' 在全局别名的多个程序集中定义