将集合与 webapi 结合使用 asp.net mvc 5



我在将集合添加到 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控制器中获得的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 上遇到了问题,请告知如何继续在这里。

我可以同意Ruard的观点,这是一个自动映射器映射问题。您只是将发布映射到发布DTO。您还应该将 PublicationFilePath 映射到 PublicationFilePathsDTO。(另请注意,您在 DTO 中复数了"路径到路径"一词(

在自动映射器中配置嵌套映射在其 Wiki 部分中进行了说明。

希望这对您有所帮助。

问候威廉

最新更新