Copied to clipboard

Flag this post as spam?

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


  • vad9ch 11 posts 41 karma points
    Mar 31, 2022 @ 18:27
    vad9ch
    0

    Creating API Controller with GET mothods

    Hi guys. You helped me last time, I generate models in my VS using Model Builder. Also, you give me a link to Umbraco video courses.

    Now I'm creating controller that will contain few GET methods. Maybe I'll look stupid but I`ll ask - where i could read about creating API controller with get methods(to get models from umbraco)

  • Corné Hoskam 81 posts 588 karma points MVP 3x c-trib
    Apr 01, 2022 @ 07:25
    Corné Hoskam
    0

    Hi vad9ch,

    Never be afraid to ask questions! Perhaps the following entry on the Umbraco Docs can be of use to you! đŸ™‚

    https://our.umbraco.com/documentation/Reference/Routing/WebApi/

    Kind regards,

    Corné Hoskam

  • vad9ch 11 posts 41 karma points
    Apr 01, 2022 @ 08:41
    vad9ch
    0

    Ty, I appreciate it. Maybe you will help with my, I wish the last question. I create models in DocTypes and generate them in my CODE. In this controller, I only need to GET these models(default .tolist and .tolist where I should get the model with ID parameter) from Umbraco DB(i guess?). These 2 methods are on my screenshot down below. enter image description here

    I`ll be glad for any answer.

  • Corné Hoskam 81 posts 588 karma points MVP 3x c-trib
    Apr 01, 2022 @ 09:30
    Corné Hoskam
    100

    Hi vad9ch,

    No Umbraco Content ever needs to be requested directly from a database. Umbraco has services & internal API's that allow you to fetch the data that you desire! Think of things like the ContentCache, ContentService, MemberService!

    Here are a couple more links I would think would be useful to look through, to give you some examples on how to fetch Content! đŸ™‚

    https://our.umbraco.com/documentation/Fundamentals/Code/Umbraco-Services/

    https://our.umbraco.com/documentation/reference/management/services/contentservice/

    https://apidocs.umbraco.com/v9/csharp/api/Umbraco.Cms.Core.Services.IContentService.html

    Hope these can help you through your journey of Umbraco!

    Corné Hoskam

  • vad9ch 11 posts 41 karma points
    Apr 01, 2022 @ 13:05
    vad9ch
    0

    Hi again. I don't get it.

    Maybe I should give a bigger picture about my current "Hello World" project. I create 2 models in DocTypes - Car Brand and Car Model. I read about tree structure but I found only how to do a content tree for Views and Forms but not for models.(That's my first big problem with project).

    After this i generate models into CODE: enter image description here

    I create 2 exemplars of my Models in Umbraco content editor: enter image description here

    Now I'm struggling to create an API controller to GET these models. All i need - 2 GEt Methods. I read info in this link you gave me: https://our.umbraco.com/documentation/Fundamentals/Code/Umbraco-Services/ but i don't found anything about how to get model exemplars.

  • Corné Hoskam 81 posts 588 karma points MVP 3x c-trib
    Apr 01, 2022 @ 13:18
    Corné Hoskam
    0

    Hi vad9ch,

    My apologies, perhaps I should do some clarification first! You started off by creating two new Document Types, which represents your (data)-Model of your Umbraco content. Using the Umbraco ModelsBuilder you have the option to create a strongly typed version of said (data-)Model.

    When you create nodes under the Content section, you create 1..n item(s) that are based off of the Document Type that you have defined under the Settings section. This does not mean that you need to create 1 Document Type for every Content Item that you wish to create, only when they either differ in their data structure, or you want to separate them for other purposes.

    What you are trying to achieve in your example, would be to fetch both of the Content Nodes from the Content Section and have them returned as a GET request. An example of this can be found in the Umbraco-Services above.

    Let's say you wanted to retrieve the first content item in Umbraco that uses the DocType "carBrand" in your case. An example of how you could request said item could be as follows:

     using (var contextReference = _contextFactory.EnsureUmbracoContext())
            {
                IPublishedContentCache contentCache = contextReference.UmbracoContext.Content;
                IPublishedContent carBrandItem = contentCache.GetAtRoot().FirstOrDefault().Children.FirstOrDefault(f => f.ContentType.Alias == "carBrand");
                if (carBrandItem == null)
                {
                    _logger.LogDebug("carBrandItem Not Found");
                }
                return carBrandItem.Name;
            }
    

    The variable cardBrandItem will now be an instance of IPublishedContent (the interface all ModelsBuilder Content Items inherit), that contains all the properties you & Umbraco added. If you want to access your custom properties, like Icon or Foundation Date, you can cast the cardBrandItem to the CarBrand model that was generated by Umbraco! đŸ™‚

    Hope this clarifies some things!

    Corné Hoskam

  • vad9ch 11 posts 41 karma points
    Apr 01, 2022 @ 17:18
    vad9ch
    0

    Hello, Corné.

    Thank you for your help. I appreciate it. Yeah, my models are different and should work like: Each Car Model should belong to only one Car Brand (tree structure); (but I don't know how to create a tree with models in my doctypes)

    Models in DocTypes

    But, uh. I copy the code in your last answer. I get a response from the postman that no exemplars exist.

    Thats how my Controller looks like: Thats how my Controller looks like

  • vad9ch 11 posts 41 karma points
    Apr 01, 2022 @ 17:19
    vad9ch
    0

    I really appreciate your help ;)

Please Sign in or register to post replies

Write your reply to:

Draft