首页 > 解决方案 > How to inject IUriHelper service in Blazor?

问题描述

How to add implementation of IUriHelper service to Startup.cs in Blazor?

标签: asp.net-coreblazorblazor-server-side

解决方案


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:

enter image description here


推荐阅读