Access the actual constructed PublishedContent in BlockList
I am trying to access the model of the current page being rendered in side a blocklist template. The model is being constructed by a RenderController
I have the following structure.
Home
Apps
App
When a user navigates to /apps/app1?id=1 The page is rendered using a IContentFinder.
public class DefaultAppContentFinder : IContentFinder
{
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
public DefaultAppContentFinder(IUmbracoContextAccessor umbracoContextAccessor)
{
_umbracoContextAccessor = umbracoContextAccessor;
}
public bool TryFindContent(IPublishedRequestBuilder request)
{
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var context)) return false;
var path = request.Uri.AbsolutePath;
var query = request.Uri.Query;
var cache = context.Content;
if (!path.StartsWith("/apps/")) return false;
if (string.IsNullOrEmpty(query) || !query.Contains("id=")) return false;
var match = cache.GetAtRoot()
.FirstOrDefault()
?.DescendantsOrSelf<App>()
.FirstOrDefault();
if (match == null) return false;
request.SetPublishedContent(match);
return true;
}
}
A for this route a RenderController fetches the additional app info and return a instance of AppDetailViewModel(which inherits from the App Model)
public class AppController : RenderController
{
private readonly IAppService _appService;
private readonly IUmbracoMapper _mapper;
private readonly IVariationContextAccessor _variationContextAccessor;
private readonly ServiceContext _serviceContext;
public AppController(ILogger<RenderController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor, IAppService appService, IUmbracoMapper mapper, ServiceContext serviceContext, IVariationContextAccessor variationContextAccessor)
: base(logger, compositeViewEngine, umbracoContextAccessor)
{
_appService = appService;
_mapper = mapper;
_serviceContext = serviceContext;
_variationContextAccessor = variationContextAccessor;
}
[HttpGet]
public async Task<IActionResult> Index([FromQuery]Guid? id)
{
var app = await _appService.GetAsync(id);
var vm = new AppDetailViewModel(CurrentPage, new PublishedValueFallback(_serviceContext, _variationContextAccessor))
{
App = _mapper.Map<AppViewModel>(app)
};
return CurrentTemplate(vm);
}
}
So far all is good. In the App.cshtml i have the instance of AppDetailViewModel.
But in the BlockList template when i retrieve the content item from Umbraco.AssignedContentItem i get the instance of the App which was set by the IContentFinder(I assume)
var page = Umbraco.AssignedContentItem; //instance of App
In the comments there is mention of AssignedContentItem not being the actual item being rendered by the UmbracoContext.
How do access the instance of AppDetailViewModel in the BlockList?
hi - Ive been injecting IUmbracoContextAccessor and then using this:
var contentIpc = this.umbracoContextAccessor.GetRequiredUmbracoContext().PublishedRequest.PublishedContent;
not sure its the official/best way but at the time I was looking into it I couldn't find any documentation. Seems like it needs to be more prominent/convenient to get the current ipc (within a controller or viewcomponent etc)
Access the actual constructed PublishedContent in BlockList
I am trying to access the model of the current page being rendered in side a blocklist template. The model is being constructed by a RenderController
I have the following structure.
When a user navigates to /apps/app1?id=1 The page is rendered using a IContentFinder.
A for this route a RenderController fetches the additional app info and return a instance of AppDetailViewModel(which inherits from the App Model)
So far all is good. In the App.cshtml i have the instance of AppDetailViewModel.
But in the BlockList template when i retrieve the content item from Umbraco.AssignedContentItem i get the instance of the App which was set by the IContentFinder(I assume)
In the comments there is mention of AssignedContentItem not being the actual item being rendered by the UmbracoContext.
How do access the instance of AppDetailViewModel in the BlockList?
Thanks for any help.
hi - Ive been injecting IUmbracoContextAccessor and then using this:
not sure its the official/best way but at the time I was looking into it I couldn't find any documentation. Seems like it needs to be more prominent/convenient to get the current ipc (within a controller or viewcomponent etc)
Hey Andrew,
Thanks for helping out. I tried your recommendation.
In the blocklist template i added.
But this isnt the AppDetailViewModel instance that was returned as the Model in AppController.
is working on a reply...