无法序列化AnonymousType。请考虑使用DataContractAttribute属性对其进行标记

时间:2013-06-20 17:44:23

标签: c# asp.net-mvc asp.net-web-api entity-framework-5

我有一个Web API,我只是想以XML格式将一些数据返回给调用客户端。

我一直收到以下错误:

<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>

这是控制器代码:

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);
        }
    }

我的第一个问题是,如何将返回对象(通过API)默认为XML?因此,每当有人访问该链接时,他们都会获得XML。

第二个问题是,我如何将上面代码中的匿名类型作为XML返回给客户端?

2 个答案:

答案 0 :(得分:3)

为该类型命名,使其不再是匿名的:

[DataContract]
public class Show
{
     public string DataSource {get; set;}
     public string Title {get; set;}
  ... etc.
}

然后

select new Show
    {
        DataSource = "Tribune",
        Title = shows.Title,
        EpisodeId = "",
        EpisodeTitle = "",
        Genre = shows.Category,
        ShowTypeDescription = showTypes.ShowTypeDescription,
        DirectorName = shows.DirectorName,
        ReleaseYear = shows.ReleaseYear,
        SeasonEpisode = ""
    };

答案 1 :(得分:0)

只需创建一个类型并选择该类型的新实例而不是匿名对象。

至于XML类型......你不想这样做。 ApiController的优点在于它将序列化为客户想要的任何类型。如果他们说Accept:text / xml他们会得到xml。如果他们说Accept:text / json他们会得到JSON。