If a view contains a RenderSection
it is considered to be a layout page and cannot be rendered directly. See this question for more info: The file "~/Views/Position/Edit.cshtml" cannot be requested directly because it calls the "RenderSection" method
The easiest solution is to merge the partial view into the layout particularly if _Header.cshtml is only included on the layout page.
Another option is to override the header view with a custom header when needed.
In your layout page:
@if (IsSectionDefined("Header"))
{
RenderSection("Header");
}
else
{
@Html.Partial("_Header");
}
In your view (with custom icons):
@section Header
{
@Html.Partial("_CustomHeader")
}
A further solution is to derive all your models from a base model class that contains custom icon data, e.g.
public abstract class BaseModel
{
public List<Icon> Icons { get; set; }
}
public class ModelWithIcons : BaseModel
{
public ModelWithIcons()
{
// Set up icon data
}
}
The _header partial view is (still defined in the layout page) will use this base model and, with null checks, render any icons it finds:
@model BaseModel
<!-- Header HTML -->
@if (Model != null && Model.Icon)
{
// Render icons
}
<!-- More HTML -->
The obvious downside here is that all your models must derive from the BaseModel class or the partial view will throw an error.