Created
April 6, 2021 16:08
-
-
Save crgrieve/02734ed89417ec0cea9e6e2b3aca0bdd to your computer and use it in GitHub Desktop.
Umbraco dotnet core API controller with annotations
This file contains 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 | |
{ | |
[ApiController] | |
[Route("umbraco/api/[controller]")] | |
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