【问题标题】:Using collections with webapi asp.net mvc 5使用带有 webapi asp.net mvc 5 的集合
【发布时间】:2017-05-21 15:25:01
【问题描述】:

我在将集合添加到 webapi 控制器时遇到了一些困难:

我有一个“出版物”类:

public class Publications
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public double Price { get; set; }
    public int RoomMates { get; set; }
    public string City { get; set; }
    public string Street { get; set; }
    public string HouseNumber { get; set; }
    public DateTime? AddDate { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
    public virtual ICollection<PublicationFilePath> PublicationFilePaths { get; set; }

}

还有一个“PublicationFilePath”类,其中包含链接到上述类的出版物的所有文件路径:

public class PublicationFilePath
{
    public int PublicationFilePathId { get; set; }
    [StringLength(255)]
    public string FileName { get; set; }
    public FileType FileType { get; set; }
    public virtual Publications Publication { get; set; }
}

我创建了以下 DTO:

 public class PublicationsDTO
{
    public int Id { get; set; }
    public string Title { get; set; }        
    public double Price { get; set; }
    public string City { get; set; }
    public DateTime? AddDate { get; set; }
    public ICollection<PublicationFilePath> PublicationFilePaths { get; set; }

}

public class PublicationsDetailDTO
{

    public int Id { get; set; }
    public string Title { get; set; }

    public double Price { get; set; }
    public int RoomMates { get; set; }
    public string City { get; set; }
    public string Street { get; set; }
    public string HouseNumber { get; set; }
    public DateTime? AddDate { get; set; }
    public string UserID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public ICollection<PublicationFilePath> PublicationFilePaths { get; set; }

}
public class PublicationsFilePathsDTO
{
    public int PublicationFilePathId { get; set; }
    public string FileName { get; set; }
}

最后这是我的 webapi 控制器使用 Automapper 的 GET 方法:

// GET: api/PublicationsAPI
    public IQueryable<PublicationsDTO> GetPublications()
    {
        Mapper.Initialize(cfg => cfg.CreateMap<Publications, PublicationsDTO>());

        var publications = db.Publications.Include(p => p.PublicationFilePaths).ProjectTo<PublicationsDTO>();

        return publications;
    }

但是,在运行代码并请求 api 时,我收到以下错误:

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
</ExceptionMessageExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace />
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type System.Data.Entity.DynamicProxies.PublicationFilePath_A0D9D29395534E1AFC4F8E51FE41FB7B06CBE02E84BBB7D3C75B4E5DE116D45D met gegevenscontractnaam PublicationFilePath_A0D9D29395534E1AFC4F8E51FE41FB7B06CBE02E84BBB7D3C75B4E5DE116D45D:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies wordt niet verwacht. U kunt dit probleem omzeilen door DataContractResolver te gebruiken of door typen die niet bekend zijn op statische wijze aan de lijst met bekende typen toe te voegen, bijvoorbeeld door het kenmerk KnownTypeAttribute te gebruiken of door ze toe te voegen aan de lijst met bekende typen die aan DataContractSerializer worden doorgegeven.
</ExceptionMessage>

我认为我的 Icollection 有问题,请在此处告知如何继续。

【问题讨论】:

  • 您似乎没有映射 PublicationFilePathDTO。所以它使用的是实体框架 PublicationFilePath。
  • 感谢您的评论 Ruard,您能否指出我忘记映射 PublicationFilePathDTO 的位置​​?我对此很陌生。谢谢!
  • 在 PublicationsDTO 和 PublicationsDetailDTO 中,您都映射到 ICollection(EF 对象)而不是 ICollection
  • 再次感谢 Ruard,这确实有道理!我应该如何从 Publications api 控制器更改 GET 方法?我想我需要添加两个映射,但我不确定如何执行此操作。
  • 我不明白你的意思。为什么必须更改 GET 方法?

标签: asp.net-mvc asp.net-web-api automapper dto


【解决方案1】:

我同意 Ruard 的观点,这是 Automapper 映射问题。 您只是将 Publications 映射到 PublicationsDTO。 您还应该将 PublicationFilePath 映射到 PublicationFilePathsDTO。 (另请注意,您在 DTO 中将 Path to Paths 一词复数)

在 AutoMapper 中配置嵌套映射在 their Wiki-section 中进行了说明。

希望对你有所帮助。

问候 威廉

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多