返回模型中包含模型列表的MVC Web API错误

时间:2019-07-17 08:11:48

标签: c# asp.net-web-api model-view-controller

我有一个带有多个属性的模型,其中一个属性是另一个模型列表,如下所示。

    public class Api_Catalog
        {
            public long catalog_id { get; set; }
            public string catalog_title { get; set; }
            public DateTime? catalog_date { get; set; }
            public string catalog_category { get; set; }
            public string catalog_lang { get; set; }
            public int? catalog_pageno { get; set; }
            public string catalog_thumb { get; set; }
            public List<CONTENTS_TBL> catalog_contents { get; set; } = new List<CONTENTS_TBL>();
        }

并在api函数内部填充它们并返回。

public Api_Catalog Api_Catalog_DL(long id)
    {


        using (var db = new J_DBEntities())
        {
            var lang = "";

            var cat_item = db.CATALOG_TBL.Find(id);
            var contents = db.CONTENTS_TBL.Where(x => x.CATALOG_FK == id).ToList();

            if (cat_item.CATALOG_RTL_FLAG == true)
            {
                lang = "فارسی";

            }
            else
            {
                lang = "انگلیسی";

            }

            var apiCat_Temp = new Api_Catalog
            {
                catalog_id = cat_item.CATALOG_ID,
                catalog_title = cat_item.CATALOG_TITLE,
                catalog_category = cat_item.CATALOG_TITLE,
                catalog_pageno = cat_item.CATALOG_PAGENO,
                catalog_date = cat_item.CATALOG_DATE,
                catalog_lang = lang,
                catalog_contents = contents

            };

            return (apiCat_Temp);

        }


    }

对于不具有内容的项目,一切正常,但是当该项目具有内容列表时,它将返回错误。

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace/>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type 'System.Data.Entity.DynamicProxies.CONTENTS_TBL_F82E24086E4B397B23C3FEFBEFA46E78769AE8B5C828EDA885E60A9B1D22B8A0' with data contract name 'CONTENTS_TBL_F82E24086E4B397B23C3FEFBEFA46E78769AE8B5C828EDA885E60A9B1D22B8A0:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.
</ExceptionMessage>
<ExceptionType>
System.Runtime.Serialization.SerializationException
</ExceptionType>
<StackTrace>
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WriteArrayOfCONTENTS_TBLToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract ) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WriteApi_CatalogToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract ) at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpContent.<CopyToAsyncCore>d__44.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__22.MoveNext()
</StackTrace>
</InnerException>
</Error>

我不确定,但是我认为内部列表应该在猜中单独序列化,对于没有内容列表的项目,它可以使用任何方式。

<Api_Catalog xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Javad_New.Models">
<catalog_category>کاتالوگ</catalog_category>
<catalog_contents/>
<catalog_date>2019-06-18T00:00:00</catalog_date>
<catalog_id>36</catalog_id>
<catalog_lang>انگلیسی</catalog_lang>
<catalog_pageno>10</catalog_pageno>
<catalog_thumb i:nil="true"/>
<catalog_title>کاتالوگ </catalog_title>
</Api_Catalog>

0 个答案:

没有答案