http://localhost:5000
. Open the address in your browser to view it.
appsettings.json
file.ProjectId
key with your environment’s ID.http://localhost:5000
in your browser, the app will work with data from the specified project.
\Models\ContentTypes\
folder with your own models.
KontentModelGenerator
tool.
dotnet tool run KontentModelGenerator
with the following parameters:
--projectid
– identifies an environment of your Kontent.ai project.--withtypeprovider
– tells the generator to create the CustomTypeProvider
class to support runtime type resolution.--generatepartials
– tells the generator to create partial classes for easier customization.--structuredmodel
– tells the generator to type rich text elements as IRichTextContent
objects instead of strings.--outputdir
– specifies where to store the generated models.--namespace
– specifies the namespace of the generated models..Generated
and another without the suffix. The suffixed files are partial classes, and suffix-less files are for your custom code.
This means you can extend the models in separate files and re-generate them in the future without losing your code. Learn more about the data types used for individual elements.
HomeController
controller.
Use the same approach to map your content to the models of your content types. Make sure you adjust the following when retrieving content:
Article
with the name of the class representing your content type.article
in the EqualsFilter
parameter with the codename of your content type.OrderParameter
so that the API orders the results by values in existing elements or system attributes.Html.DisplayFor
helper method in your Views.
By default, the blocks of content in rich text elements will be rendered using their default HTML representations, which match what you get from the Delivery API. For better results, we recommend that you write your own resolvers of rich text content.
<ModelName>.cshtml
where the ModelName
refers to the content type of the content item or component. For example, Tweet.cshtml
.InlineImage.cshtml
.Resolvers\CustomContentLinkUrlResolver.cs
file in your app and implement these two methods:
ResolveLinkUrl()
for linked content items that are available.ResolveBrokenLinkUrl()
for linked content items that are not available, for example, unpublished or deleted items.dotnet new --install "Kontent.Ai.Boilerplate::*"
dotnet new kontent-ai-mvc --name "MyKontentAiWebsite"
cd .\MyKontentAiWebsite
dotnet run
# Installs the Kontent.ai model generator tool locally into the app's folder
dotnet tool restore
# Note: This overwrites existing files in the .\Models\ContentTypes folder
dotnet tool run KontentModelGenerator --projectid "<YOUR_ENVIRONMENT_ID>" --withtypeprovider true --structuredmodel true --outputdir ".\Models\ContentTypes" --namespace "MyWebsite.Models" --generatepartials true
// Retrieves 3 articles ordered by their post date
public async Task<ViewResult> Index()
{
var response = await DeliveryClient.GetItemsAsync<Article>(
new EqualsFilter("system.type", "article"),
new LimitParameter(3),
new DepthParameter(0),
new OrderParameter("elements.post_date")
);
// Sends the strongly-typed content items to a View
return View(response.Items);
}
@using MyWebsite.Helpers.Extensions
@model Article
<div class="body">
<h2>@Model.Title</h2>
<!-- Generates an IMG element for the image using a helper method. -->
@Html.AssetImage(@Model.TeaserImage.First(), title: @Model.TeaserImage.First().Name, cssClass: "img-responsive", sizes: new ResponsiveImageSizes(300)
.WithMediaCondition(new MediaCondition { MinWidth = 330, MaxWidth = 768, ImageWidth = 689 }))>
@Model.Summary
</div>
@* Renders content of a rich text element (BodyCopy in an article) *@
@Html.DisplayFor(model => model.BodyCopy)
public Task<string> ResolveLinkUrlAsync(IContentLink link)
{
// Resolves links pointing to Article content items
if (link.ContentTypeCodename.Equals(Article.Codename))
{
return Task.FromResult($"/articles/{link.UrlSlug}");
}
// Add the rest of the resolver logic
}
public Task<string> ResolveBrokenLinkUrlAsync()
{
// Resolves URLs to unavailable content items
return Task.FromResult("/Error/404");
}
\Models\ContentTypes\
folder.
The Article model is referenced as an example in multiple locations across the app:
\Controllers\HomeController.cs
\Controllers\SiteMapController.cs
\Views\Home\Index.cshtml
\Views\Shared\DisplayTemplates\Article.cshtml