IPublishedContent.ChildrenForAllCultures - Only re...
# help-with-umbraco
x
Hi, i could really use a hand on this. With such code:
Copy code
cs
var culture = HttpContext.Features.Get<IRequestCultureFeature>()!.RequestCulture.Culture;
using var umbracoContext = _umbracoContextFactory.EnsureUmbracoContext().UmbracoContext;

var blog = (Blog)umbracoContext.Content!.GetByRoute("/blog", culture: culture.Name)!;

var response = MapBlogToResponse(blog, culture.Name);
return Ok(response);
When looking into the debugger, i only get one child one, but that child node has a list of cultures, which has two elements? https://cdn.discordapp.com/attachments/1264204453129752576/1264204453305647104/image.png?ex=669d05a5&is=669bb425&hm=fe3c9ead22043e309c73ee85ae65ff5d1889209a4fbc55253c04e81eee473c17&
This then breaks the rest of the query, because the cultures are not loaded for the the mapping.
Full context:
Copy code
cs
using System.Diagnostics.CodeAnalysis;

using Asp.Versioning;

using MaGyRakovina.Api.Controllers.Common;
using MaGyRakovina.Contracts.Blog;

using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;

using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.PublishedModels;

namespace MaGyRakovina.Api.Controllers;

[VersionedRoute("blog")]
public class BlogController : ApiController
{
    private readonly IUmbracoContextFactory _umbracoContextFactory;
    
    public BlogController(IUmbracoContextFactory umbracoContextFactory)
    {
        _umbracoContextFactory = umbracoContextFactory;
    }

    [MapToApiVersion(1)]
    [HttpGet("categories")]
    public IActionResult GetBlogCategories()
    {
        var culture = HttpContext.Features.Get<IRequestCultureFeature>()!.RequestCulture.Culture;
        using var umbracoContext = _umbracoContextFactory.EnsureUmbracoContext().UmbracoContext;

        var blog = (Blog)umbracoContext.Content!.GetByRoute("/blog", culture: culture.Name)!;

        var response = MapBlogToResponse(blog, culture.Name);
        return Ok(response);
    }

    [SuppressMessage("ReSharper", "ConvertToLambdaExpression")]
    private BlogCategoriesResponse MapBlogToResponse(Blog blog, string culture)
    {
        return new BlogCategoriesResponse(blog.Children(culture)
            .Cast<BlogCategory>()
            .Select(category =>
            {
                return new BlogCategoryResponse(
                    category.Title,
                    category.Children(culture)
                        .Cast<BlogArticle>()
                        .Select(article =>
                        {
                            return new BlogArticleThumbnailResponse(
                                article.Title,
                                article.Key);
                        })
                        .ToArray()
                );
            })
            .ToArray());
    }
}
j
I'm facing a similar problem, did you get anywhere with this?
Hi Because you are trying to get the content within an API controller, you need to set the culture manually with
IVariationContextAccessor.
See here https://our.umbraco.com/forum/using-umbraco-and-getting-started/109807-localization-problem-using-viewcomponents-with-ajax-in-umbraco910#comment-340414
23 Views