Copied to clipboard

Flag this post as spam?

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


  • Kåre Mai 29 posts 207 karma points
    Nov 16, 2022 @ 10:02
    Kåre Mai
    0

    Getting strongly typed content inside controller

    Hi,

    I can't seem to find any documentation helping me in understanding how to get strongly typed content inside my own custom api controller.

    This is what i have for now:

    using Microsoft.AspNetCore.Mvc;
    using Umbraco.Cms.Web.Common.Controllers;
    using Umbraco.Cms.Web.Common;
    using Umbraco.Cms.Web.Common.PublishedModels;
    
    namespace universe_web.Custom_Controllers.Api
    {
        public class ExperiencesController : UmbracoApiController
        {
            private readonly UmbracoHelper _umbracoHelper;
    
            public ExperiencesController(UmbracoHelper umbracoHelper)
                => _umbracoHelper = umbracoHelper;
    
            [HttpGet]
            public Oplevelse[] GetAll()
            {
                var selection = _umbracoHelper.Content(Guid.Parse("777c01de-34ab-4630-a2e7-e8bf2b40b35e")).ChildrenOfType("oplevelse");
    
                return selection;
            }
        }
    }
    

    As you can see, i would like to return an array of strongly typed "Oplevelse" objects (auto converted to json by .net). However i can't seem to find out how i can turn the .Content query into a list of "Oplevelse" objects instead of the generic IPublishedContent.

  • Conor Howe 36 posts 137 karma points
    Nov 16, 2022 @ 20:36
    Conor Howe
    0

    Have you tried to cast the IPublishedContent to a models builder model?

  • Kåre Mai 29 posts 207 karma points
    Nov 16, 2022 @ 21:39
    Kåre Mai
    0

    Hi,

    Yes that is my problem, I can't seem to figure out how to properly cast this to an array of "Oplevelse".

    A different approach would be to not use the auto generated classes and make an "Oplevelse" class myself and then manually mapping these objects. This would also enable me to use the in-memory model builder option instead but would involve more work creating the classes.

  • Conor Howe 36 posts 137 karma points
    Nov 16, 2022 @ 22:13
    Conor Howe
    0

    You could add an 'as IEnumerable< T > ' to the end, or try an expression such as what is described here:

    https://our.umbraco.com/forum/extending-umbraco-and-using-the-api/96571-model-builder-how-to-covert-ipublishedcontent-in-to-strongly-typed-model

    There are a few others that can be used to cast also, but typically an 'as T' works well

  • Kåre Mai 29 posts 207 karma points
    Nov 17, 2022 @ 08:09
    Kåre Mai
    0

    Thanks Conor!

    The .OfType<> did the trick!

    However it won't work as the generated types from the model builder cannot be converted to json objects:

    NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported.
    
    System.Text.Json.Serialization.Converters.UnsupportedTypeConverter<T>.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
    NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported. Path: $.Ekstra.ContentType.PropertyTypes.ModelClrType.
    
    System.Text.Json.ThrowHelper.ThrowNotSupportedException(ref WriteStack state, NotSupportedException ex)
    

    So it seems the generated classes can't directly be used to create an API.

    I also can't find any documentation on API creation. All i can find is that Umbraco decided to stop support for their own rest api and a very sparse documentation on creating an api controller but not on how to then actually publish data.

    Is there any documentation on this anywhere?

  • Conor Howe 36 posts 137 karma points
    Nov 17, 2022 @ 10:08
    Conor Howe
    0

    That makes complete sense, you may have to deal with two different cases here then. Get the data from Umbraco as you currently have, then map that data to a new class that can be used as the json response. Probably better from SRP outlook too.

    I can see they have this documentation: https://our.umbraco.com/documentation/Reference/Routing/Umbraco-API-Controllers/

    Not sure if this is the one you are referencing?

  • Kåre Mai 29 posts 207 karma points
    Nov 17, 2022 @ 10:21
    Kåre Mai
    0

    Yes that is the doc. i'm referencing :)

    I ended up taking the route of creating my own objects and mapping which has the benefit that i can again set the modelbuilder to be in-memory and also, as you describe, save a lot on the json data as i can map only the properties that i need!

    Thanks for all the inputs and help.

  • Conor Howe 36 posts 137 karma points
    Nov 17, 2022 @ 12:43
    Conor Howe
    0

    No problem - glad its helped 😊

Please Sign in or register to post replies

Write your reply to:

Draft