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)
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.
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! đŸ™‚
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:
I create 2 exemplars of my Models in Umbraco content editor:
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! đŸ™‚
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)
But, uh. I copy the code in your last answer. I get a response from the postman that no exemplars exist.
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)
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
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.
I`ll be glad for any answer.
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
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:
I create 2 exemplars of my Models in Umbraco content editor:
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.
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:
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
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)
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:
I really appreciate your help ;)
is working on a reply...