dotnet / dotnet/aspnetcore

[Blazor] Provide a way to acquire reference to currently routed page component instance

Open
#64,025 2 comments 0 reactions 0 assignees View on GitHub
area-blazor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

Currently, the `` only supplies the page *type* that has been routed to, not the instance itself.

I'm trying to define a way for my pages to supply metadata to the overall application. For example, a page might describe SEO options, breadcrumbs, or additional site-specific data.

``` razor
@page "/hello"
@implement IHasBreadcrumbs

Hello

@code {
public IEnumerable GetInitialBreadcrumbs() => [ new("Hello") ]
}
```

~Currently, there's no supported way to actually get hold of the instance that has been instantiated by the router. The Router itself doesn't even capture the reference to the page when instantiating the component~

Or rather, the current `RouteView` and its descendants don't supply the reference to the instantiated component (it's not the `Router` instantiating the component, but one of the `RouteView` descendants):
https://github.com/dotnet/aspnetcore/blob/9f255d1ed217c0081e9b602fa86411b1821c2e0f/src/Components/Components/src/RouteView.cs#L86-L96

### Describe the solution you'd like

~Provide a way to capture the reference of the current page component:~
- ~in the `Found` `RenderFragment` (`RouteData`) in the `Router`, or~
- ~in the `LocationChangedEventArgs` arguments in the `NavigationManager.LocationChanged` event callback.~

~It would allow for inspecting and actually calling the logic defined in the page in the above model-driven scenario.~

Expose the captured component instance through the `RouteView` type for consumption.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.