Block list route hijacking
# help-with-umbraco
j
Hello everybody, I have a blocklist item, I want to put extra information and logic before the block renders but I cannot find out how to add a controller before this happens. I have it working with normal pages, but the same does not work for blocks. Umbraco 12
d
Hi Jeroen! In the modern Umbraco versions, we no longer use controllers and child actions. However, you are still able to do the same thing using ViewComponents. You can find more information about view components here: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-7.0
Now if you want to Invoke a view component inside a block list, you can do so inside the
default.cshtml
view in the blocklist folder. A common approach is to search for a viewcomponent with the same name as the alias of the block type like this:
Copy code
@inject Microsoft.AspNetCore.Mvc.ViewComponents.IViewComponentSelector selector
@foreach (var block in Model)
{
    var data = block.Content;
    var contentTypeAlias = data.ContentType.Alias;
    if (selector.SelectComponent(contentTypeAlias) != null)
    {
        @await Component.InvokeAsync(contentTypeAlias, new { model = block })
    }
    else
    {
        <partial name="blocklist/grid/@contentTypeAlias" model="@block.Content" />
    }
}
j
Thank you, this is wat I was looking for
3 Views