Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 将集合与webapi asp.net mvc 5一起使用_Asp.net Mvc_Asp.net Web Api_Automapper_Dto - Fatal编程技术网

Asp.net mvc 将集合与webapi asp.net mvc 5一起使用

Asp.net mvc 将集合与webapi asp.net mvc 5一起使用,asp.net-mvc,asp.net-web-api,automapper,dto,Asp.net Mvc,Asp.net Web Api,Automapper,Dto,向webapi控制器添加集合时遇到一些困难: 我有一门“出版物”课: 我创建了以下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

向webapi控制器添加集合时遇到一些困难:

我有一门“出版物”课:

我创建了以下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; }
}
公共类发布SDTO
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共双价{get;set;}
公共字符串City{get;set;}
公共日期时间?AddDate{get;set;}
公共ICollection PublicationFilePath{get;set;}
}
公共类出版物详细信息
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共双价{get;set;}
公共int室友{get;set;}
公共字符串City{get;set;}
公共字符串Street{get;set;}
公共字符串HouseNumber{get;set;}
公共日期时间?AddDate{get;set;}
公共字符串用户标识{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共ICollection PublicationFilePath{get;set;}
}
公共类发布文件路径SDTO
{
public int PublicationFilePathId{get;set;}
公共字符串文件名{get;set;}
}
最后,这是使用Automapper从我的webapi控制器获取方法:

// 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;
    }
//GET:api/PublicationsAPI
公共IQueryable GetPublications()
{
初始化(cfg=>cfg.CreateMap());
var publications=db.publications.Include(p=>p.publicationfilepath).ProjectTo();
归还出版物;
}
但是,在运行代码和请求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>

发生了一个错误。
'ObjectContent'1'类型未能序列化内容类型'application/xml'的响应正文;字符集=utf-8'。
System.InvalidOperationException异常
发生了一个错误。
键入System.Data.Entity.DynamicProxies.PublicationFilePath_A0D9D29395534E1AFC4F8E51FB7B06CBE02E84BBB7D3C75B4E5DE116D45D符合GEGEGEVENSCONTRACTNAAM PublicationFilePath_A0D9D29395534E1AFC4E51FB7B06CBE02E84BBB7D3C75B4DE116D45D:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies wordt niet verwacht。如果有问题,则门数据合同的解决者在操作统计时遇到了门数据合同的类型,而门数据合同的解决者遇到了数据合同的类型。

我想我对Icollection有问题,请建议如何在这里继续。

我同意Ruard的观点,这是一个自动映射问题。 您仅将发布映射到PublicationsTo。 您还应该将PublicationFilePath映射到PublicationFilePathsDTO。 (还请注意,您将单词Path多元化为DTO中的路径)

中介绍了在AutoMapper中配置嵌套映射

希望这对你有所帮助

问候
William

似乎您没有将PublicationFilePathDTO映射到。所以它使用的是实体框架PublicationFilePath。谢谢你的评论,你能指出我忘记将PublicationFilePath映射到哪里了吗?这方面我还不太熟悉。非常感谢。在PublicationsTo和PublicationsDetailDTO中,您都可以映射到ICollection(EF对象),而不是ICollection。再次感谢Ruard,这确实很有意义!如何从发布api控制器更改GET方法?我想我需要添加两个映射,但我不知道该怎么做。我不明白你的意思。为什么要更改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;
    }
<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>