Inject NavigationManager in razor.
@inject NavigationManager NavigationManager
Use Uri from NavigationManager to get the current URL.
string currentUrl = NavigationManager.Uri;
Sample code
@page "/sample"
@inject NavigationManager NavigationManager
<p>@currentUrl</p>
@code {
private string currentUrl;
protected override void OnInitialized()
{
currentUrl = NavigationManager.Uri;
}
}
Share with