代码改变世界

Error:You must write an attribute 'type'='object' after writing the attribute with local name '__type'

2012-05-16 14:13  Wizardlsw  阅读(718)  评论(1编辑  收藏  举报

昨天在使用WebApi的时候,遇到这个错误。

You must write an attribute 'type'='object' after writing the attribute with local name '__type'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Xml.XmlException: You must write an attribute 'type'='object' after writing the attribute with local name '__type'.

代码如下:

        public HttpResponseMessage<List<ArticleSimple>> GetArticles(DateTime date)
        {
            //DONE returns top 10 articles
            ArticleService service = new ArticleService();

            var articles = service.GetLatestArticleSimplesByDate(date, null);

            //DONE using viewModel that do not display Metadata and some useless properties
            return new HttpResponseMessage<List<ArticleSimple>>(articles, System.Net.HttpStatusCode.OK);           
        }

 

解决方法:

        有两种,其中一种简单的是在实例化DbContext时指定 context.Configuration.ProxyCreationEnabled = false;

参考:

       http://forums.asp.net/p/1773173/4848023.aspx/1?You+must+write+an+attribute+type+object+after+writing+the+attribute+with+local+name+__type+

       http://msdn.microsoft.com/zh-cn/library/bb738612