Use the Uri
property from the NavigationManager
class.
How it works
Get it from injection before using it on .razor
pages:
@inject NavigationManager MyNavigationManager
Or like this in a .cs
file if you prefer the “code-behind” experience:
using Microsoft.AspNetCore.Components;
// ...
[Inject]
public NavigationManager MyNavigationManager {get; set;} = default!;
Sample
@page "/navigate"
@inject NavigationManager MyNavigationManager
<h1>Current URL</h1>
<p>@(MyNavigationManager.Uri)</p>
More about navigation (NavigateTo, BaseUri, ToAbsoluteUri, ToBaseRelativePath, … ) at: URI and navigation state helpers
NavigationManager cheatsheet
MyNavigationManager.Uri
#> https://localhost:5001/counter/3?q=hi
MyNavigationManager.BaseUri
#> https://localhost:5001/
MyNavigationManager.NavigateTo("http://new location")
#> Navigates to new location
MyNavigationManager.LocationChanged
#> An event that fires when the navigation location has changed.
MyNavigationManager.ToAbsoluteUri("pepe")
#> https://localhost:5001/pepe
MyNavigationManager.ToBaseRelativePath(MyNavigationManager.Uri)
#> counter/3?q=hi
Helper: AddQueryParm( "q2", "bye" ) // (*1)
#> https://localhost:5001/counter/3?q=hi&q2=bye
Helper: GetQueryParm( "q" )
#> hi
(*1)
Net6 introduces GetUriWithQueryParameter
. More info: Manipulate the query string from Blazor
Helpers code:
@code {
[Parameter]
public string Id { get; set; }
// Blazor: add parm to URL
string AddQueryParm(string parmName, string parmValue)
{
var uriBuilder = new UriBuilder(MyNavigationManager.Uri);
var q = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
q[parmName] = parmValue;
uriBuilder.Query = q.ToString();
var newUrl = uriBuilder.ToString();
return newUrl;
}
// Blazor: get query parm from the URL
string GetQueryParm(string parmName)
{
var uriBuilder = new UriBuilder(MyNavigationManager.Uri);
var q = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
return q[parmName] ?? "";
}
}