使用Web API返回复杂对象 [英] Return complex objects with Web API

查看:152
本文介绍了使用Web API返回复杂对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



所以我确定我错过了一些相当基本的东西,但我不能为我的生活找出它是什么。



我有以下设置:

界面:

Hi Guys,

So I'm sure I'm missing something fairly basic but I cannot for the life of me figure out what it is.

I have the following set up:
Interface:

public interface IMyFileInterface
{
    string FileName {get;set;}
    MyEnum FileType {get;set;
    DateTime UploadDate {get;set;}
}



课程:


Classes:

[DataContract]
public class ReturnModel
{
   [DataMember]
   public string Name {get;set;}
   [DataMember]
   public List<IMyFileInterface> LatestFiles {get;set;}
}
[DataContract]
public class FooFileModel : IMyFileInterface
{
    [DataMember]
    string FileName {get;set;}
    [DataMember]
    MyEnum FileType {get;set;
    [DataMember]
    DateTime UploadDate {get;set;}

}





我有一个web api控制器,其函数返回ReturnModel



然而,当我调用它时,我收到以下错误:



输入'Common.Objects.FooFileModel'数据协定名称为'FooFileModel:http://schemas.datacontract.org/2004/07/Common.Objects'不是预期的。考虑使用DataContractResolver或将任何静态未知的类型添加到已知类型列表中 - 例如,使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中。



我尝试将KnownType属性添加到LatestFiles属性但是错误并说它只能添加到对象中,所以我尝试将它添加到FooFileModel中已知的类型是接口,但又没有区别。



所以我的问题是,如何设置我的类和接口,以便它们可以传递回客户端应用查看web api?我确定我错过了一些简单的东西,但我不能为我的生活弄清楚。



任何帮助都将不胜感激。



关注



I have a web api controller with a function that returns the ReturnModel

However, when I call it I get the following error:

Type 'Common.Objects.FooFileModel' with data contract name 'FooFileModel:http://schemas.datacontract.org/2004/07/Common.Objects' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

I've tried adding the KnownType attribute to the LatestFiles property but that errors and says it can only be added to an object, so I tried adding it to the the FooFileModel with the known type being the interface but again that has made no difference.

So my question is, how can I set up my classes and interfaces so they can be passed back to a client app view the web api? I'm sure I'm missing something simple, but I cannot for the life of me figure it out.

Any help would be greatly appreciated.

Regards

推荐答案

之前添加! ReturnModel这一行:[KnownType(typeof(FooFileModel))]

Add before! ReturnModel this line: [KnownType(typeof(FooFileModel))]
[KnownType(typeof(FooFileModel))]
[DataContract]
public class ReturnModel
{
   [DataMember]
   public string Name {get;set;}
   [DataMember]
   public List<IMyFileInterface> LatestFiles {get;set;}
}



原因是ReturnModel具有类型的属性,即接口。这不是运行时类型,序列化是使用运行时类型完成的......

它是如何完成的?底层框架(WebAPI的数据协定)尝试在实际序列化之前加载所有相关的运行时类型。在接口的情况下,它无法完成... KnownType的那一行指示框架也加载FooFileModel供以后使用...

如果你打算使用更多实现接口的类你将不得不为每个添加一个KnwonType ...


The reason is that ReturnModel has a property of type that is an interface. That is not a run-time type and serialization is done using run-time types...
How it done? The underlying framework (that of data contract of WebAPI) try to load all the relevant run-time types BEFORE actual serialization. In the case of interface it can not be done...That line of KnownType instructs the framework to load also FooFileModel for later use...
If you intend to use more classes that implement the interface you will have to add a KnwonType lone for each...


这篇关于使用Web API返回复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆