Umbraco-CMS icon indicating copy to clipboard operation
Umbraco-CMS copied to clipboard

UmbracoContext.PublishedRequest is null, using UmbracoPageController

Open merijng opened this issue 4 months ago • 2 comments

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

14.3.0

Bug summary

IUmbracoContextFactory in Umbraco is not functioning as expected in Umbraco versions 14.3.0 and 13.x.x. PublishedRequest is null. This issue does not occur in Umbraco version 12.3.10.

Specifics

  • Affected versions: Umbraco 13.x.x and Umbraco 14.3.0
  • Working version: Umbraco 12.3.10
  • Environment: IUmbracoContextFactory to access PublishedRequest in UmbracoPageController.

Steps to reproduce

Install Umbraco 12.3.10 using:

   dotnet new install Umbraco.Templates::12.3.10
   dotnet new umbraco -n MyProject
  • Run the application and add a controller similar to TestController (as below) using the UmbracoPageController and the FindContent method.
  • Also register the controller (as below) to WithEndpoints, within Startup.cs.
  • Run the application and access the /test/{id} URL.
  • The test should pass as expected.

Install Umbraco 14.3.0 following the Umbraco documentation.

  • Add the same TestController implementation. Add the endpoint in Program.cs
  • Run the application and access the /test/{id} URL.
  • The test will fail with a null PublishedRequest.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.Controllers;

public class TestController : UmbracoPageController, IVirtualPageController
{
    private readonly IUmbracoContextFactory _umbracoContextFactory;

    public TestController(
        ILogger<UmbracoPageController> logger,
        ICompositeViewEngine compositeViewEngine,
        IUmbracoContextFactory umbracoContextFactory)
        : base(logger, compositeViewEngine)
    {
        _umbracoContextFactory = umbracoContextFactory;
    }

    public IPublishedContent FindContent(ActionExecutingContext actionExecutingContext)
    {
        using (var reference = _umbracoContextFactory.EnsureUmbracoContext())
        {
            if (reference.UmbracoContext.PublishedRequest == null)
                throw new Exception("Test failed");
            else
                throw new Exception("Test passed");
        }
    }

    [HttpGet]
    public async Task<IActionResult> Index(string id = null)
    {
        throw new Exception("Test passed");
    }
}
u.EndpointRouteBuilder.MapControllerRoute(
   nameof(TestController),
   "/test/{id?}",
   new { Controller = "Test", Action = "Index" });

Expected result / actual result

  • PublishedRequest not being null.

merijng avatar Oct 08 '24 14:10 merijng