How to add implementation of IUriHelper
service to Startup.cs in Blazor?
Asked
Active
Viewed 6,804 times
6

Dmitry
- 549
- 1
- 5
- 16
-
1Are you talking about your own implementation? – dani herrera Sep 29 '19 at 14:10
-
No. When I'm using @Inject IUriHelper I'm getting InvalidOperationException: Cannot provide a value for property 'uriHelper' on type 'WebUI.Shared.NavMenu'. There is no registered service of type 'Microsoft.AspNetCore.Blazor.Services.IUriHelper'. – Dmitry Sep 29 '19 at 14:42
-
ok @sideCode, see my answer. – dani herrera Sep 29 '19 at 16:04
2 Answers
20
IUriHelper
is now NavigationManager
. See Get current Url in a Blazor component for details.
You can inject and use NavigationManager
at Shared/NavMenu.cshtml
without issues. You don't need any expecial, you don't need to add NavigationManager
at app startup, it's already injected. Sample:
Shared/NavMenu.cshtml:
@inject NavigationManager NavigationManager
<div class="top-row pl-4 navbar navbar-dark">
<a class="navbar-brand" href="">
@(NavigationManager.Uri) @* <--- sample using it --- *@
</a>
<button class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
</div>
Result:

dani herrera
- 48,760
- 8
- 117
- 177