简体   繁体   English

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

[英]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.

相关问题 考虑使用DataContractAttribute属性标记它? - Consider marking it with the DataContractAttribute attribute? 类型“ MyClass”不能从未标记DataContractAttribute或SerializableAttribute的类型继承。 考虑标记基本类型 - Type 'MyClass' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute. Consider marking the base type 类型不能ISerializable,并且具有DataContractAttribute属性 - Type cannot be ISerializable and have DataContractAttribute attribute 发生内部异常:类型 &#39;&lt;&gt;f__AnonymousType2`6[...]&#39; 无法序列化 - Internal exception has occured: Type '<>f__AnonymousType2`6[...]' cannot be serialized 序列化视图模型时出错:“类型&#39;System.Web.HttpPostedFileWrapper&#39;无法序列化。” - Error serializing view model: “Type 'System.Web.HttpPostedFileWrapper' cannot be serialized.” DataContract运行时错误-类型&#39;myType&#39;无法序列化。 我做错了什么? - DataContract runtime error - Type 'myType' cannot be serialized. What i'm doing wrong? 无法转换类型&#39;AnonymousType#1&#39; - Cannot convert type 'AnonymousType#1' 无法从&#39;AnonymousType#1&#39;转换为&#39;int&#39; - cannot convert from 'AnonymousType#1' to 'int' 修改LINQ输出无法分配AnonymousType - Modify LINQ output AnonymousType cannot be assigned 无法隐式将匿名类型#1转换为对象 - Cannot Implicitly convert anonymoustype#1 to Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM