'Equivalent to ControllerBase's `IsLocalUrl` in Blazor

So I normally have an endpoint in a controllerbase where I check if a given url is local or not with Url.IsLocalUrl(returnUrl), but I can't find an equivalent to use in a Blazor code behind. Does this just not exist yet? Using .NET 6 if that helps.



Solution 1:[1]

Try to use NavigationManager :

@inject NavigationManager Navigation;
bool isLocal = Navigation.ToAbsoluteUri(Navigation.Uri).IsLoopback;

Solution 2:[2]

Use NavigationManager.BaseUri

@page "/base"
@page "/base/base"
@inject NavigationManager NavManager

<PageTitle>BaseUrl</PageTitle>

<div class="p-2">
    Url : @this.baseUrl
</div>

@code {
    private string baseUrl = string.Empty;

    protected override void OnInitialized()
    {
        this.baseUrl = NavManager.BaseUri;
        base.OnInitialized();
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user13256346
Solution 2 MrC aka Shaun Curtis