'ObjectContent`1'类型无法序列化内容类型'text / xml的响应正文;字符集= UTF-8'

时间:2016-08-24 17:49:16

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

老问题解决了json方面 当我请求内容类型'text / json时,我得到API的正常响应 和问题的时候  我请求内容类型'text / xml; 我收到这条消息     发生错误。在此context.System.InvalidOperationException中不能使用'ObjectContent 1' type failed to serialize the response body for content type 'text/xml; charset=utf-8'.</ExceptionMessage><ExceptionType>System.InvalidOperationException</ExceptionType><StackTrace /><InnerException><Message>An error has occurred.</Message><ExceptionMessage>There was an error generating the XML document.</ExceptionMessage><ExceptionType>System.InvalidOperationException</ExceptionType><StackTrace> at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) 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.Web.Http.WebHost.HttpControllerHandler.&lt;WriteBufferedResponseContentAsync&gt;d__1b.MoveNext()</StackTrace><InnerException><Message>An error has occurred.</Message><ExceptionMessage>The type System.Collections.Generic.List 1 [[testtransotels.data.cities,testtransotels,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]] at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name,String ns,Object o,Boolean xsiType)    在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterObject.Write1_Object(String n,String ns,Object o,Boolean isNullable,Boolean needType)    在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterObject.Write2_anyType(Object o)

我看到的代码

   private datacontext db = new datacontext();
 public object Post([FromBody]Request Request)
    {
        return db.Student;

    }
 public class Student
{
    [DataMember]

    public string StudentID { get; set; }

    [DataMember]

    public string StudentName { get; set; }
    [DataMember]

    public string StudentCode { get; set; }
}

0 个答案:

没有答案