Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Neil Hodges 338 posts 987 karma points
    May 23, 2019 @ 13:31
    Neil Hodges
    0

    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:

    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.

    Hope that makes sense.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    May 23, 2019 @ 13:40
    Michaël Vanbrabandt
    0

    Hi Neil,

    can you try the following:

    var homeNode = rootNodes.FirstOrDefault(x => x.DocumentTypeAlias == "home").Select(y => new Home(y));
    

    Your PublishedContentModel has a constructor excepting a IPublishedContent which comes from your query.

    Hope this helps!

    Have a nice day.

    /Michaël

  • Neil Hodges 338 posts 987 karma points
    May 23, 2019 @ 14:03
    Neil Hodges
    0

    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:

    var homeNode = rootNodes.Where(x => x.DocumentTypeAlias == "home").Select(y => new Home(y)).FirstOrDefault();
    

    But again get an error:

    "Message": "An error has occurred.",
        "ExceptionMessage": "Self referencing loop detected with type 'Concierge.Core.Models.Home'. Path 'ContentSet[0].ContentSet'.",
        "ExceptionType": "Newtonsoft.Json.JsonSerializationException",
    
  • Jonathan Distenfeld 105 posts 618 karma points
    May 23, 2019 @ 14:32
    Jonathan Distenfeld
    0

    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?

            dynamic exObj = new System.Dynamic.ExpandoObject();
            var dictionary = exObj as IDictionary<string, object>;
            foreach (var prop in Model.Content.Properties)
            {
                var umbracoNodes = prop.Value as List<IPublishedContent>;
    
                if (umbracoNodes != null)
                {
                    var tmpDictionaries = new List<dynamic>();
                    foreach(var umbracoNode in umbracoNodes)
                    {
                        tmpDictionaries.Add(GetProperties(umbracoNode.Properties));
                    }
                    dictionary[prop.PropertyTypeAlias] = tmpDictionaries;
                    continue;
                }
                dictionary[prop.PropertyTypeAlias] = prop.Value.ToString();
            }
            return JsonConvert.SerializeObject(exObj);
    

    Source: https://gist.github.com/EdCharbeneau/702a4c2d702d30f371bd

    ~Jonathan

  • Neil Hodges 338 posts 987 karma points
    May 23, 2019 @ 14:59
    Neil Hodges
    0

    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:

    var sezObj = JsonConvert.SerializeObject(exObj);
    Home hh = JsonConvert.DeserializeObject(sezObj);
    

    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.

Please Sign in or register to post replies

Write your reply to:

Draft