Skip to content

Instantly share code, notes, and snippets.

@crgrieve
Created April 6, 2021 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crgrieve/973359ddb7c638e9a99eba94382111ef to your computer and use it in GitHub Desktop.
Save crgrieve/973359ddb7c638e9a99eba94382111ef to your computer and use it in GitHub Desktop.
Umbraco dotnet core controller
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using Umbraco.Cms.Core;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Extensions;
using Umbracov9AlphaAPIs.APIModels;
namespace Umbracov9AlphaAPIs.Controllers
{
public class BlogController : UmbracoApiController
{
private readonly IPublishedContentQuery _contentQuery;
public BlogController(IPublishedContentQuery contentQuery)
{
_contentQuery = contentQuery;
}
[HttpGet]
public List<BlogPostModel> GetBlogPosts()
{
List<BlogPostModel> topics = new();
var blogs = _contentQuery.ContentSingleAtXPath("//home/blogRepo").Children;
foreach (var blog in blogs)
{
topics.Add(new()
{
Title = blog.GetProperty("blogTitle").GetValue().ToString(),
Preview = blog.GetProperty("blogPreview").GetValue().ToString(),
Url = blog.Url(),
PublishedDate = blog.CreateDate
});
}
return topics;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment