Location
The URL of the current document, split into typed parts: read protocol, host, path, query and hash, or navigate by assigning to them - every setter triggers a real navigation.
@inject Bit.Butil.Location locationMDN reference
Read the URL parts
GetHref / GetProtocol / GetHost / GetHostname / GetPort / GetPathname / GetSearch / GetHash / GetOriginEach component of the current URL has its own getter, so there is no string parsing on your side - the browser does the splitting.
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"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.
await location.SetHash("demo-section");
string hash = await location.GetHash(); // "#demo-section"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.
await location.Reload();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.
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 httpsSetHash, 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.
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
Task<string> GetHref() / Task SetHref(string value)Task<string> GetProtocol() / Task SetProtocol(string value)Task<string> GetHost() / Task SetHost(string value)Task<string> GetHostname() / Task SetHostname(string value)Task<string> GetPort() / Task SetPort(string value)Task<string> GetPathname() / Task SetPathname(string value)Task<string> GetSearch() / Task SetSearch(string value)Task<string> GetHash() / Task SetHash(string value)Task<string> GetOrigin()Task Assign(string url)Task Reload()Task Replace(string url)Task<string[]> GetAncestorOrigins()