So I have a scenario where my Umbraco is creating Models via ModelBuilder and I'm building them automatically into a Class Library.
Within said Class Library id like to have some API Controllers that return back a generated Model.
I have this code below:
public JsonResult<Home> ConciergeHome()
{
var rootNodes = Umbraco.TypedContentAtRoot();
var homeNode = rootNodes.FirstOrDefault(x => x.DocumentTypeAlias == "home") as Home;
return Json(homeNode);
}
Using 'Postman' to check this API call I get an error returning the model here:
return Json(homeNode);
The error is:
"Message": "An error has occurred.",
"ExceptionMessage": "Self referencing loop detected with type 'Concierge.Core.Models.Home'. Path 'ContentSet'.",
"ExceptionType": "Newtonsoft.Json.JsonSerializationException",
What's the best way to return this generated model so I can de-searialise it into JSON and then re-serialise it in my APS.NET Core App which is calling the API?
I want the API to be agnostic so that the ASP.Net Core app i have can serialise it back into the Model which is in the shared class library.
Return ModelsBuilder via API Controller
Hi
So I have a scenario where my Umbraco is creating Models via ModelBuilder and I'm building them automatically into a Class Library.
Within said Class Library id like to have some API Controllers that return back a generated Model.
I have this code below:
Using 'Postman' to check this API call I get an error returning the model here:
The error is:
What's the best way to return this generated model so I can de-searialise it into JSON and then re-serialise it in my APS.NET Core App which is calling the API?
I want the API to be agnostic so that the ASP.Net Core app i have can serialise it back into the Model which is in the shared class library.
Hope that makes sense.
Hi Neil,
can you try the following:
Your PublishedContentModel has a constructor excepting a
IPublishedContent
which comes from your query.Hope this helps!
Have a nice day.
/Michaël
HI Michaël
Thanks for your response.
The 'Select' part says 'can not resolve symbol Select' I've added in using System.Linq; but still showing red.
Ive tried:
But again get an error:
Hi Neil,
Looks like the Umbraco Models can't be converted into json that easy. There is some circular dependency in there. Maybe this could help?
Source: https://gist.github.com/EdCharbeneau/702a4c2d702d30f371bd
~Jonathan
Hi Jonathan
Thank you for your response.
It looks Ok, however, I'm not sure I could Deserialise it back into the same Generated model within my ASP.Net Core App. Using something like:
I think what I'm trying to achieve is some way in part Umbraco Headless, albeit self-hosted rather than the Saas cloud headless.
I'm thinking my only other way would be to map the 'Generated Model' to my own constructed View Model, which mirrors the generated Model.
This kinda defeats the point though of having generated models if I have to manually map each one to my own model though.
is working on a reply...