Sections support in Blazor
See original GitHub issueBasically the same as https://github.com/dotnet/aspnetcore/issues/10131 which has been closed in in favour of #10452 which was in turn closed in favour of #10450 (influencing the <head>
).
Having the ability to embed content from the page in the layout in more than one placeholder is a common need. A typical use case is to display the page name in the “header” of the application which is usually defined in the layout. Another use case is to display personalised content for the currently logged user.
A lot of other technologies support this feature and I think it would be a great addition to Blazor. A few examples:
- ASP.NET WebForms has ContentPlaceholder
- ASP.NET MVC has sections
- Web Pages also have sections
- Angular has router outlets
- React Router also supports multiple components
Thank you for your consideration!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:40
- Comments:23 (9 by maintainers)
Top Results From Across the Web
ASP.NET Core Blazor sections
To control the content in a Razor component from a child Razor component, Blazor supports sections using the following built-in components:.
Read more >Control head content in ASP.NET Core Blazor apps
Learn how to control head content in Blazor apps, including how to set the page title from a component.
Read more >ASP.NET 8 Blazor Sections Fully Explained
Sections can be defined by string name or by using a strongly typed object. A strongly typed SectionId can be used to support...
Read more >Blazor Sections 🔥 New Feature in .NET 8
NET 8 Blazor Sections 01:49 Implementation of Sections 06:20 ... Experimental multi-threading support for Blazor WebAssembly (coming in .
Read more >The Blazor Power Hour: Sections, SectionOutlet, and ...
Let's take a deep dive into a .NET 8 Preview feature, sections. What are SectionOutlet, and SectionContent? How do we use them to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
One of the reasons we haven’t got this as a baked-in feature yet is that it’s possible to implement something like this in your own application code. If there’s enough consensus that some single pattern takes care of almost all requirements, we might well consider putting it into the framework. But until then, it would help if people were able to try out ways of doing this and report back on what’s working well or not well for them.
To get started, here’s one way to do it:
SectionContent
,SectionOutlet
, andSectionRegistry
) from this Gist to your project: https://gist.github.com/SteveSandersonMS/4f08efe2ad32178add12bfa3eb6e4559<SectionOutlet>
with a name to define where you want some content to go. For example, you could add the following into the default template’sMainLayout.razor
:<SectionContent>
with a name to define some content to be rendered. For example, you changeCounter.razor
to render the following:Now when the user navigates to “Counter”, they will have a button in the navigation bar that updates the count. And when they navigate to any other page, that button will disappear.
Of course, each page could provide its own different content for
topbar
, and you can have any number of different named sections.Technically you can even use this approach to supply content to the
<head>
element in your page (e.g., to have a page-specific<title>
) - I checked this works. It’s a bit awkward because you do need to migrate everything inside<html>
to be part of yourApp.razor
root component so the<head>
is rendered by Blazor. But it can be done. If this sort of approach pleases people we can look into ways of making it more natural in .NET 6.Thank you @SteveSandersonMS ! This is a viable solution.
Indeed it is. But the implementation is so simple and small that you can consider including it in Blazor (or another official Nuget package that people can use). Thanks again for the code and general idea. I will use it in my projects.