umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.42k stars 2.67k forks source link

Custom UmbracoPageController using custom routes can no longer get UmbracoContext #16969

Open jamesrichardbrett opened 3 weeks ago

jamesrichardbrett commented 3 weeks ago

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

13.4.0

Bug summary

After upgrading to Umbraco 13.4.0 can no longer get the Umbraco Context in a Custom UmbracoPageController

An unhandled exception occurred while processing the request. InvalidOperationException: Wasn't able to get an UmbracoContext Umbraco.Extensions.UmbracoContextAccessorExtensions.GetRequiredUmbracoContext(IUmbracoContextAccessor umbracoContextAccessor)

Downgrading back to 13.3.1 fixes the issue

here is the code

public class RobotsTxtController : UmbracoPageController { public static string[] RoutePatterns = new[] { "/robots.txt", "/{local}/robots.txt" };

private readonly IPublishedValueFallback _publishedValueFallback;
private readonly IUmbracoContextFactory _umbracoContextFactory;

public RobotsTxtController(ICombinedLogger<RobotsTxtController> logger, ICompositeViewEngine compositeViewEngine,
        IPublishedValueFallback publishedValueFallback,
        IUmbracoContextFactory umbracoContextFactory)
    : base(logger.Logger, compositeViewEngine)
{
    _publishedValueFallback = publishedValueFallback;
    _umbracoContextFactory = umbracoContextFactory;
}

public IActionResult Index()
{
    var robotsDefaultContent = "User-agent: *\n Disallow: /";

    using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
    {
        if (CurrentPage is SiteRoot root && root.Value<bool>("allowCrawlers"))
        {
            var overrideRobotstxtContent = root.Value<string>(_publishedValueFallback, "overrideRobotstxtContent");

            if (!string.IsNullOrWhiteSpace(overrideRobotstxtContent))
            {
                robotsDefaultContent = overrideRobotstxtContent;
            }
            else
            {
                robotsDefaultContent = "User-agent: *\nDisallow: /app_plugins/\nDisallow: /umbraco/";
            }

            var sitemap = CurrentPage?.FirstChild<Sitemap>();
            if (sitemap != null)
            {
                robotsDefaultContent = robotsDefaultContent + $"\n\nSitemap: {sitemap.Url(mode: UrlMode.Absolute)}";
            }
        }
    }

    return Content(robotsDefaultContent, "text/plain", Encoding.UTF8);
}

}

Composer

public class RobotsComposer : IComposer { public void Compose(IUmbracoBuilder builder) { builder.Services.Configure(options => { options.AddFilter(new UmbracoPipelineFilter(nameof(RobotsTxtController)) { Endpoints = app => app.UseEndpoints(endpoints => { for (int i = 0; i < RobotsTxtController.RoutePatterns.Length; i++) { endpoints.MapControllerRoute( $"{nameof(RobotsTxtController)}_{i}", RobotsTxtController.RoutePatterns[i], new { Controller = ControllerExtensions.GetControllerName(), Action = nameof(RobotsTxtController.Index) }) .ForUmbracoPage(FindContent); } }) }); }); }

private IPublishedContent FindContent(ActionExecutingContext actionExecutingContext)
{
    // Resolve services from the container
    var umbracoContextFactory = actionExecutingContext.HttpContext.RequestServices
        .GetRequiredService<IUmbracoContextFactory>();

    using (UmbracoContextReference umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext())
    {
        var umbracoContext = umbracoContextReference.UmbracoContext;

        var domain = DomainUtilities.SelectDomain(umbracoContext.Domains?.GetAll(false), umbracoContext.CleanedUmbracoUrl);

        if (domain == null)
            return DefaultToFirstRoot(umbracoContext);

        var content = umbracoContext.Content?.GetById(domain.ContentId);
        return content ?? DefaultToFirstRoot(umbracoContext);
    }
}

private IPublishedContent DefaultToFirstRoot(IUmbracoContext umbracoContext)
{
    return umbracoContext.Content?.GetAtRoot().FirstOrDefault() ?? throw new InvalidOperationException("Umbraco has no content nodes to find");
}

}

Specifics

This currently work fine in live which is still 13.3.1 DEV and UAT environments on 13.4.0 both error

Steps to reproduce

Visit the page /robots.txt

Expected result / actual result

No response

github-actions[bot] commented 3 weeks ago

Hi there @jamesrichardbrett!

Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.

We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.

We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.

Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face: