loading

Read the URL parts

GetHref / GetProtocol / GetHost / GetHostname / GetPort / GetPathname / GetSearch / GetHash / GetOrigin

Each component of the current URL has its own getter, so there is no string parsing on your side - the browser does the splitting.

C#
string href = await location.GetHref();         // full URL
string protocol = await location.GetProtocol(); // "https:"
string host = await location.GetHost();         // "example.com:5001"
string hostname = await location.GetHostname(); // "example.com"
string port = await location.GetPort();         // "5001"
string pathname = await location.GetPathname(); // "/location"
string search = await location.GetSearch();     // "?q=butil"
string hash = await location.GetHash();         // "#section"
string origin = await location.GetOrigin();     // "https://example.com:5001"
Live sample
URL parts output
Results will appear here when you interact with the samples.

Change the hash

GetHash / SetHash

The fragment is the one URL part you can write without causing a page load, which makes it a handy slot for lightweight client state such as the active section.

C#
await location.SetHash("demo-section");

string hash = await location.GetHash();   // "#demo-section"
Live sample
New hash
hash output
Results will appear here when you interact with the samples.

Reload

Reload

Reloads the current URL, exactly like the browser's Refresh button. In Blazor WebAssembly this restarts the whole application, so unsaved in-memory state is lost.

C#
await location.Reload();
Live sample
reload output
Results will appear here when you interact with the samples.

Rewrite individual parts

SetProtocol / SetHost / SetHostname / SetPort / SetPathname / SetSearch

Every non-hash setter navigates to the URL produced by swapping just that component - a concise way to hop between ports, subdomains or query strings while keeping the rest of the address intact.

C#
await location.SetPathname("/window");     // same site, different page

await location.SetSearch("?theme=dark");   // same page, new query string

await location.SetPort("5001");            // same URL, different port
await location.SetHostname("staging.example.com");
await location.SetHost("staging.example.com:5001");
await location.SetProtocol("https");       // upgrade to https
Warning:
Setters navigate immediately Except for SetHash, every setter on this page (and Assign, Replace, Reload, SetHref) triggers a full document navigation that tears down the running Blazor application. Persist anything important before calling them.
Note:
Location vs NavigationManager For in-app route changes, Blazor's own NavigationManager is usually the better fit - it keeps the SPA alive. Reach for Location when you need the parsed URL parts or a real full-page navigation, such as leaving to an external site or forcing a clean reload.

API reference

Member
Signature
Description
GetHref / SetHref
Task<string> GetHref() / Task SetHref(string value)
The entire URL; setting it navigates to the new page.
GetProtocol / SetProtocol
Task<string> GetProtocol() / Task SetProtocol(string value)
The protocol scheme including the final ':'; setting it navigates.
GetHost / SetHost
Task<string> GetHost() / Task SetHost(string value)
The hostname plus ':' and port; setting it navigates.
GetHostname / SetHostname
Task<string> GetHostname() / Task SetHostname(string value)
The domain of the URL; setting it navigates.
GetPort / SetPort
Task<string> GetPort() / Task SetPort(string value)
The port number of the URL; setting it navigates.
GetPathname / SetPathname
Task<string> GetPathname() / Task SetPathname(string value)
The path beginning with '/'; setting it navigates.
GetSearch / SetSearch
Task<string> GetSearch() / Task SetSearch(string value)
The query string beginning with '?'; setting it navigates.
GetHash / SetHash
Task<string> GetHash() / Task SetHash(string value)
The fragment beginning with '#'; the only setter that does not reload the page.
GetOrigin
Task<string> GetOrigin()
The canonical origin of the location (read-only).
Assign
Task Assign(string url)
Loads the resource at the given URL, adding a history entry.
Reload
Task Reload()
Reloads the current URL, like the Refresh button.
Replace
Task Replace(string url)
Navigates to the given URL, replacing the current history entry.
GetAncestorOrigins
Task<string[]> GetAncestorOrigins()
Origins of the frames containing this document, innermost first; empty in a top-level page. Not implemented in Firefox, where it also reports empty - so pair it with Window.IsInIframe before treating empty as proof you are not framed.
An unhandled error has occurred. Reload 🗙