c# - AnonymousType 无法序列化。考虑用 DataContractAttribute 属性标记它

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

Controller 代码如下:

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 返回给客户端?

最佳答案

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

[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 = ""
    };

关于c# - AnonymousType 无法序列化。考虑用 DataContractAttribute 属性标记它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17220330/

相关文章:

c# - 有什么办法可以取消鼠标点击事件

python - 如何最适合在网络服务器上运行的API检查其公共(public)可用性/状态

c# - 使用 IPrincipal 集对 ASP.net Web API 进行单元测试

c# - Web API 2 中的 OData 具有可嵌入的资源链接

c# - 如何获取从泛型类继承的集合的所有类型?

c# - 如何解决: The provided anti-forgery token was meant for a different claims-based user than the current user

c# - 使用 Ajax 自动填充 DropDownList

asp.net-mvc - 模板 ASP.NET MVC View

JQuery 将数据发送到 MVC 操作方法?

c# - 如何创建全局变量并使其可用于 ASP.NET MVC 5 框架中的所有 View ?