[英]AnonymousType cannot be serialized. Consider marking it with the DataContractAttribute attribute
I have a Web API and I'm just trying to return some data to the calling client in XML format. 我有一个Web API,我只是想将一些数据以XML格式返回给调用客户端。
I keep getting the following error: 我不断收到以下错误:
<ExceptionMessage>
Type '<>f__AnonymousType7`9[System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.Nullable`1[System.Int32],System.String]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
</ExceptionMessage>
Here's the Controller code: 这是控制器代码:
public class TribuneShowsController : ApiController
{
private readonly TVDataEntities db;
public TribuneShowsController()
{
db = new TVDataEntities();
db.Configuration.ProxyCreationEnabled = false;
}
public IEnumerable GetTribuneShows(string title = null,
string genre = null,
string showTypeDescription = null,
string directorName = null,
string releaseYear = null)
{
var query = from shows in db.TRIB_Shows
from showTypes in
db.TRIB_LKP_ShowTypes.Where(v => v.ShowTypeCode == shows.ShowTypeCode).DefaultIfEmpty()
select new
{
dataSource = "Tribune",
shows.Title,
EpisodeId = "",
EpisodeTitle = "",
Genre = shows.Category,
showTypes.ShowTypeDescription,
shows.DirectorName,
shows.ReleaseYear,
SeasonEpisode = ""
};
if (title != null)
{
query = query.Where(s => s.Title.Contains(title));
}
if (genre != null)
{
query = query.Where(s => s.Genre.Contains(genre));
}
if (showTypeDescription != null)
{
query = query.Where(s => s.ShowTypeDescription.Contains(showTypeDescription));
}
if (directorName != null)
{
query = query.Where(s => s.DirectorName.Contains(directorName));
}
if (releaseYear != null)
{
query = query.Where(s => s.ReleaseYear.ToString().Contains(releaseYear));
}
return query.ToList();
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
My first question is, how do I default the return object (by the API) to XML? 我的第一个问题是,如何将返回对象(通过API)默认为XML? So whenever someone goes to the link, they'll get an XML. 因此,只要有人访问链接,他们就会获得XML。
Second question is, how do I go about returning the anonymous type in my code above as an XML to the client? 第二个问题是,如何将上面代码中的匿名类型作为XML返回给客户端?
Give the type a name so it's no longer anonymous: 为类型指定一个名称,这样它就不再是匿名的:
[DataContract]
public class Show
{
public string DataSource {get; set;}
public string Title {get; set;}
... etc.
}
then 然后
select new Show
{
DataSource = "Tribune",
Title = shows.Title,
EpisodeId = "",
EpisodeTitle = "",
Genre = shows.Category,
ShowTypeDescription = showTypes.ShowTypeDescription,
DirectorName = shows.DirectorName,
ReleaseYear = shows.ReleaseYear,
SeasonEpisode = ""
};
Just make a type and select new instances of that type instead of an anonymous object. 只需创建一个类型并选择该类型的新实例即可,而不是匿名对象。
As for the XML type... you don't want to do that. 至于XML类型,您不想这样做。 The beauty of the ApiController is that it will serialize to whatever type the client wants. ApiController的优点在于它将序列化为客户端想要的任何类型。 If they say Accept: text/xml they will get xml. 如果他们说Accept:text / xml,他们将得到xml。 If they say Accept: text/json they will get JSON. 如果他们说Accept:text / json,他们将获得JSON。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.