Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • andrew shearer 510 posts 659 karma points
    Sep 10, 2024 @ 20:51
    andrew shearer
    0

    accessing current Published Content from ViewStart in Umbraco 13

    Hi - I wanted to dynamically set the main Layout View used based on some doctype hierarchy rules using the current content context. I thought the special aspnet ViewStart would be a good place for this logic to reside.

    However, this didn't work:

    var currentpage = umbraco.AssignedContentItem;
    

    with the error: InvalidOperationException: Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent.

    but this does:

    var currentpage = _umbracoContextAccessor?.GetRequiredUmbracoContext()?.PublishedRequest?.PublishedContent;
    

    The first approach feels "nicer" but anyone able to explain the technicallies of why that doesn't work but the context accessor way does?

    thanks

Please Sign in or register to post replies

Write your reply to:

Draft