Created
April 6, 2021 15:48
-
-
Save crgrieve/973359ddb7c638e9a99eba94382111ef to your computer and use it in GitHub Desktop.
Umbraco dotnet core controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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