仅在Web API结果中返回选择的字段

9 浏览
0 Comments

仅在Web API结果中返回选择的字段

我有一个模型,其中包含比我网页API消费者需要的值更多。

我只想在API中传递其中的一些字段。我尝试了这段代码,但是在JSON结果中,它没有略过UpdatedBy字段,而是返回了一个空值。我该如何解决这个问题?

public class Publication
{
    [Key]
    public int PublicationID { get; set; }
    public string PublicationTitle { get; set; }
    public string Frequency { get; set; }
    public DateTime NextIssueDate { get; set; }
    public DateTime SpaceDeadline { get; set; }
    public DateTime MaterialsDeadline { get; set; }
    public DateTime CreatedDt { get; set; }
    public string CreatedBy { get; set; }
    public DateTime UpdatedDt { get; set; }
    public string UpdatedBy { get; set; }
}
public IQueryable GetPublications()
{
    return db.Publications
        .ToList()
        .Select(p => new Publication {
            PublicationID = p.PublicationID,
            PublicationTitle = p.PublicationTitle,
            Frequency = p.Frequency,
            NextIssueDate = p.NextIssueDate
        })
        .AsQueryable();
}

0