Skip to main content

Using Contensis with .Net Razor Pages

Log in to add to favourites

Requirements

Using the demo project

BASH
1
git clone https://github.com/contensis/razor-page-leif-example.git

Change directory to the Leif example folder

BASH
1
cd RazorPageLeifExample

Run with hot reloading

BASH
1
dotnet watch

How it works

C#
1
2
// Program.cs using Zengenti.Contensis.Delivery;
C#
1
2
3
4
5
6
7
// Program.cs ContensisClient.Configure(new ContensisClientConfiguration( rootUrl: "<root-url>", projectApiId: "<projectApiId>", clientId: "clientId", sharedSecret: "<sharedSecret>" ));
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
// Models/BlogPost.cs using Zengenti.Contensis.Delivery; namespace RazorPageLeifExample.Models { public class BlogPost: EntryModel { // EntryModel gives us access to the Sys object for ID public string Title { get; set; } = null!; // Null forgiving - Title can't be null public string? LeadParagraph { get; set; } public Image? ThumbnailImage { get; set; } public Person? Author => Resolve<Person>("author"); // Resolve linked entry so fields are available public Category? Category => Resolve<Category>("category"); // Resolve linked entry so fields are available public ComposedField? PostBody { get; set; } } }

Get a single blog entry by its ID

C#
1
2
// Pages/Blog.cshtml.cs var client = ContensisClient.Create();
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Pages/Blog.cshtml.cs // Set the model public BlogPost? BlogPostModel { get; set; } public void OnGet() { // Connect to the Contensis delivery API // Connection details set in /Program.cs var client = ContensisClient.Create(); // Get the id from the querystring string BlogId = HttpContext.Request.Query["id"]; // Get the entries by the id BlogPostModel = client.Entries.Get<BlogPost>(BlogId); }
C#
1
2
3
4
5
6
7
8
9
<!-- Pages/Blog.cshtml --> <div class="blog-hero"> <h1 class="blog-hero__title"> @Model.BlogPostModel.Title </h1> @if(Model.BlogPostModel.ThumbnailImage != null) { <img class="blog-hero__img" src="@("http://live.leif.zenhub.contensis.cloud" + Model.BlogPostModel.ThumbnailImage.Asset.Uri)" alt="@Model.BlogPostModel.ThumbnailImage.AltText"/> } </div>

Get a list of blogs

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Pages/Index.cshtml.cs // Set the model public PagedList<BlogPost>? BlogsPayload { get; set; } public void OnGet() { ViewData["Title"] = "Blogs"; // Connect to the Contensis delivery API // Connection details set in /Program.cs var client = ContensisClient.Create(); // Query the api for entries with a content type of "blogPost" // Get the latest versions even if not yet published var blogsQuery = new Query( Op.EqualTo("sys.contentTypeId", "blogPost"), Op.EqualTo("sys.versionStatus", "latest") ); // Get a list of entries matching the blogsQuery BlogsPayload = client.Entries.Search<BlogPost>(blogsQuery); }
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!-- Pages/Index.cshtml --> @if ((Model.BlogsPayload != null) && (Model.BlogsPayload.TotalCount > 0)) { <ul class="blogs"> @foreach (var blogItem in Model.BlogsPayload.Items) { <li class="blog-card"> <a href="@("/blog?id=" + blogItem.Sys.Id)"> <h2 class="blog-card__title mobile">@blogItem.Title</h2> @if (blogItem.ThumbnailImage != null) { <img class="blog-card__img" src="@("http://live.leif.zenhub.contensis.cloud" + blogItem.ThumbnailImage.Asset.Uri)" alt="@blogItem.ThumbnailImage.AltText" /> } <div class="related-blog__content"> <h2 class="blog-card__title desktop">@blogItem.Title</h2> <!-- Truncate text as it can sometimes be too long --> @if (blogItem.LeadParagraph != null) { <p class="blog-card__text">@blogItem.LeadParagraph.Substring(0, Math.Min(blogItem.LeadParagraph.Length, 124))&hellip;</p> } @if (blogItem.Category != null) { <span class="category">@blogItem.Category.Name</span> } </div> </a> </li> } </ul> } else { <p>No blogs found</p> }

Still need help?

New support request