AllowGet could not be cast to some Newtonsoft.Json Property.
Any idea what's going an here?
Umbraco version is the newest one.
The error when I call the Controller is:
<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 'Umbraco.Web.PublishedModels.NewsletterAdministration' 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.DataContractSerializer.GetDataContract(DataContract declaredTypeContract, Type declaredType, Type objectType) 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.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__22.MoveNext()</StackTrace>
</InnerException>
</Error>
I don’t think you can serialize the IPublishedContent, it will throw you an exception due to recursiveness. Not sure why and not sure how to fix. Personally what I have done is to map the properties that I would need into a new model and then return that in my API controllers.
So if I were you, I would do:
Create a new class
Create the properties you need: node Id, node name, seoTitle (document type property), etc. etc.
In your controller, fetch the IPublishedContent as you do.
UmbracoAuthorizedApiController and return IPublishedContent
Hi,
I just write a very simple AuthorizedApiController - i simply wanted to return a node, but this seems to be a very hard challenge.
Here is my code (concerning the second function "GetNewsletterAdminNode", the first function works fine!):
I already tried to change the return value to JSONResult. But then. I get an error in the line:
AllowGet could not be cast to some Newtonsoft.Json Property.
Any idea what's going an here?
Umbraco version is the newest one.
The error when I call the Controller is:
what happens if you just return the node? i.e. return nlNode;
Exactly the same.
Hi Thomas.
I don’t think you can serialize the IPublishedContent, it will throw you an exception due to recursiveness. Not sure why and not sure how to fix. Personally what I have done is to map the properties that I would need into a new model and then return that in my API controllers.
So if I were you, I would do:
Hope that can help you solve your problem.
Regards
Must be related to your model, it works ok for me with a simple published content object
Hi, Malthe,
I feared that this will be the only way. I hoped it would be just that simple to return an existing object. So I have to write some more code.
Anyway, thank you for your time and ideas anyone.
is working on a reply...