C
C#•8mo ago
schwartzmj

URL Parameters and/or data loading in layout files

Assume I have some pages as such: - /websites/{WebsiteId:int} - /websites/{WebsiteId:int}/faqs - /websites/{WebsiteId:int}/testimonials - /websites/{WebsiteId:int}/services They all use the same layout for navigation, as such:
@using WebsiteBuilder.Components.Layout
@inherits LayoutComponentBase
@layout MainLayout

<div class="container">
<nav>
<ul>
<li><a href="/websites/@WebsiteId">Details</a></li>
<li><a href="/websites/@WebsiteId/faqs">FAQs</a></li>
<li><a href="/websites/@WebsiteId/testimonials">Testimonials</a></li>
<li><a href="/websites/@WebsiteId/services">Services</a></li>
</ul>
</nav>
@Body
</div>

@code {
[Parameter] public int WebsiteId { get; set; }
}
@using WebsiteBuilder.Components.Layout
@inherits LayoutComponentBase
@layout MainLayout

<div class="container">
<nav>
<ul>
<li><a href="/websites/@WebsiteId">Details</a></li>
<li><a href="/websites/@WebsiteId/faqs">FAQs</a></li>
<li><a href="/websites/@WebsiteId/testimonials">Testimonials</a></li>
<li><a href="/websites/@WebsiteId/services">Services</a></li>
</ul>
</nav>
@Body
</div>

@code {
[Parameter] public int WebsiteId { get; set; }
}
How do I get the @WebsiteId URL parameter "passed up" to the Layout or resolved from the URL? Is there anything built in to ASP.NET Core 8 for this?
6 Replies
gummylick
gummylick•8mo ago
Stack Overflow
Is there any way to communicate to main layout of blazor pages
Here I want to pass some title to the mainlayout.razor page so that my components or pages can update the title of header. Is there any possible way in blazor to do this ? Any help will be apprecia...
gummylick
gummylick•8mo ago
I used it recently to define title in page, setting the value in layout
schwartzmj
schwartzmjOP•8mo ago
I'd like to essentially do the opposite - read a value in the layout from the page (in this case a URL parameter)
gummylick
gummylick•8mo ago
same.. i set the title value "this is my page" in page but the parameter is in layout so depending on page, that title changes
schwartzmj
schwartzmjOP•8mo ago
ah ok. i read about cascading parameters a bit but it seemed like not a great solution having to perform the same logic in every single child of a layout when all i want is a path parameter 😦 im probably best off just not creating a layout file and instead a layout component that i just wrap every page with. if i have to do the logic in a page anyway
gummylick
gummylick•8mo ago
since its dealing with url, you can also cascade as high up as the route parameters i believe i did something like that to determine an "active" page on menu and then depending on what it was from cascading route parameter, i set "active" in css class
Want results from more Discord servers?
Add your server