0
當我在我的應用程序中訪問/WeedAPI/
URL時,出現以下錯誤。我如何讓我的web API工作? (控制器,型號代碼遵循)使用ApiController獲取List時出現System.InvalidOperationException
完整的錯誤細節:
<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 'WeedCards.Models.Weed' 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>
<ExceptionType>
System.Runtime.Serialization.InvalidDataContractException
</ExceptionType>
<StackTrace>
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type) at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type) at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type) at System.Runtime.Serialization.XmlObjectSerializerContext.GetDataContract(Int32 id, RuntimeTypeHandle typeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WriteArrayOfWeedToXml(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.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle originalDeclaredTypeHandle, Type graphType) 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.<>c__DisplayClass7.<WriteToStreamAsync>b__6() at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)
</StackTrace>
</InnerException>
</Error>
我ApiController的子類:
public class WeedAPIController : ApiController
{
//
// GET: /WeedAPI/
public IEnumerable<Weed> GetAllWeeds()
{
return WeedData.GetWeeds();
}
}
我的數據:
public class WeedData
{
public static Dictionary<string,WeedFamily> GetFamilies(){
return new Dictionary<string,WeedFamily>
{
{"mustard",new WeedFamily("Mustard","Brassicaceae")}
,{"pigweed",new WeedFamily("Pigweed","Amaranthus")}
,{"sunflower",new WeedFamily("Sunflower","Asteraceae")}
};
}
public static List<Weed> GetWeeds(){
var Families = GetFamilies();
return new List<Weed>
{
new Weed("Hairy Bittercress","Cardamine hirsuta",Families["mustard"])
,new Weed("Little Bittercress","Cardamine oligosperma",Families["mustard"])
,new Weed("Shepherd's-Purse","Capsella bursa-pastoris",Families["mustard"])
,new Weed("Wild Mustard","Sinapis arvensis/Brassica kaber",Families["mustard"])
,new Weed("Wild Radish","Raphanus raphanistrum",Families["mustard"])
,new Weed("Radish","Raphanus sativus",Families["mustard"])
,new Weed("Redroot Pigweed","Amaranthus retroflexus",Families["pigweed"])
,new Weed("Prickly Lettuce","Lactuca serriola",Families["sunflower"])
,new Weed("Spiny Sowthistle","Sonchus asper",Families["sunflower"])
,new Weed("Annual Sowthistle","Sonchus oleraceus",Families["sunflower"])
};
}
}
謝謝。我不熟悉這種語言功能。如果其他人想知道,這裏是對c#屬性的介紹:http://msdn.microsoft.com/en-us/library/aa288059%28v=vs.71%29.aspx –
@JonathanWilson很高興它解決了。 :) – Haney