response.data.item.elements.headline.value
.
In a more complicated scenario, this approach becomes cumbersome. It forces you to remember the JSON structure of your content items and the codenames of your content elements.
To make this easier, use a Delivery SDK to map the retrieved content items to their strongly typed models.
body_text
becomes property bodyText
.// This code was generated by a kontent-generators-net tool
// (see https://github.com/kontent-ai/model-generator-net).
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// For further modifications of the class, create a separate file with the partial class.
using System;
using System.Collections.Generic;
using Kontent.Ai.Delivery.Abstractions;
namespace KontentAiModels
{
public partial class Homepage
{
public const string Codename = "homepage";
public const string BodyTextCodename = "body_text";
public const string HeadlineCodename = "headline";
public const string PictureCodename = "picture";
public IRichTextContent BodyText { get; set; }
public string Headline { get; set; }
public IEnumerable<IAsset> Picture { get; set; }
public IContentItemSystemAttributes System { get; set; }
}
}
dotnet tool install -g Kontent.Ai.ModelGenerator
KontentModelGenerator --projectid 8d20758c-d74c-4f59-ae04-ee928c0816b7 --withtypeprovider true --structuredmodel true
// Tip: Find more about .NET SDKs at https://kontent.ai/learn/net
using Kontent.Ai.Delivery;
using KontentAiModels;
// Tip: Use DI to create Delivery client https://kontent.ai/learn/net-register-client
IDeliveryClient client = DeliveryClientBuilder
.WithProjectId("8d20758c-d74c-4f59-ae04-ee928c0816b7")
.Build();
// Gets a content item by codename and maps it to the item's strongly typed model
IDeliveryItemResponse<Homepage> response = await client.GetItemAsync<Homepage>("hello_caas_world");
var homepage = response.Item;
// Use homepage
// Console.WriteLine(homepage.Headline);