Copied to clipboard

Flag this post as spam?

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


  • JacobS 7 posts 107 karma points c-trib
    May 25, 2022 @ 15:44
    JacobS
    0

    Umbraco 9 - Wasn't possible to a get a valid Snapshot getting Website property

    Currently going through some hijack routing and I can currently get into my controller, and I can get the current context through the IUmbracoContextFactory.

    When I get the content back and cast it as Website to access a property I get this odd error:

    InvalidOperationException: Wasn't possible to a get a valid Snapshot

    Example code:

        public class MyTxtController : UmbracoPageController, IVirtualPageController
    {
    
        public MyTxtController(ILogger<UmbracoPageController> logger, ICompositeViewEngine compositeViewEngine) :
            base(logger, compositeViewEngine)
        {
        }
    
        [HttpGet]
        public IActionResult Index()
        {
            var myText = (CurrentPage as Website)?.MyText;
    
            return Content(!string.IsNullOrWhiteSpace(myTxt) ? myTxt : string.Empty;
        }
    
        public IPublishedContent FindContent(ActionExecutingContext actionExecutingContext)
        {
            var umbracoContextFactory = actionExecutingContext.HttpContext.RequestServices
                .GetRequiredService<IUmbracoContextFactory>();
    
            var content = default(IPublishedContent);
            using (var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext())
            {
                var ctx = umbracoContextReference.UmbracoContext;
                content = ctx.Content.GetAtRoot(CultureInfo.CurrentCulture.Name).FirstOrDefault();
            }
    
            return content;
        }
    }
    
  • JacobS 7 posts 107 karma points c-trib
    May 26, 2022 @ 11:24
Please Sign in or register to post replies

Write your reply to:

Draft