I am using DevTrev's PartialWidgetPage to recreate the MasterPage functionality of previous Kentico versions.
The Nav and Footer get rendered into the _Layout.cshtml as expected but I have another component/widget in the master page that uses a HtmlHelper extension to write to _Layout.cshtml in a similar way to to @RenderSection() does.
The widget works fine when it is placed in a normal page but when it's in a page rendered by the PartialWidgetPage, it doesn't get written to _Layout.cshtml.
The helper extension is as follows:
public static class HtmlHelperExtensions
{
private static IHttpContextAccessor httpContextAccessor;
public static HtmlString AddResource(this IHtmlHelper htmlHelper, PageResourceType resourceType, string identifier, Func<object, HelperResult> template)
{
httpContextAccessor = Service.Resolve<IHttpContextAccessor>();
if (httpContextAccessor.HttpContext.Items[resourceType] != null)
{
((List<(Func<object, HelperResult>, string)>)httpContextAccessor.HttpContext.Items[resourceType]).Add((template, identifier));
}
else
{
httpContextAccessor.HttpContext.Items[resourceType] = new List<(Func<object, HelperResult>, string)>() { (template, identifier) };
}
return new HtmlString(string.Empty);
}
public static HtmlString RenderResources(this IHtmlHelper htmlHelper, PageResourceType resourceType)
{
httpContextAccessor = Service.Resolve<IHttpContextAccessor>();
if (httpContextAccessor.HttpContext.Items[resourceType] != null)
{
List<(Func<object, HelperResult>, string)> resources = (List<(Func<object, HelperResult>, string)>)httpContextAccessor.HttpContext.Items[resourceType];
foreach (var resource in resources.DistinctBy(r => r.Item2))
{
if (resource.Item1 != null)
{
htmlHelper.ViewContext.Writer.Write(resource.Item1(null));
}
}
}
return new HtmlString(string.Empty);
}
}
And is called in _Layout.cshtml like so:
@Html.RenderResources(PageResourceType.HeadScript)