diff --git a/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Services/UriHelper.ts b/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Services/UriHelper.ts
index 53442dae62..8e77f1a6b5 100644
--- a/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Services/UriHelper.ts
+++ b/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Services/UriHelper.ts
@@ -1,6 +1,6 @@
import { registerFunction } from '../Interop/RegisteredFunction';
import { platform } from '../Environment';
-import { MethodHandle } from '../Platform/Platform';
+import { MethodHandle, System_String } from '../Platform/Platform';
const registeredFunctionPrefix = 'Microsoft.AspNetCore.Blazor.Browser.Services.BrowserUriHelper';
let notifyLocationChangedMethod: MethodHandle;
let hasRegisteredEventListeners = false;
@@ -24,8 +24,7 @@ registerFunction(`${registeredFunctionPrefix}.enableNavigationInteception`, () =
const href = anchorTarget.getAttribute('href');
if (isWithinBaseUriSpace(toAbsoluteUri(href))) {
event.preventDefault();
- history.pushState(null, /* ignored title */ '', href);
- handleInternalNavigation();
+ performInternalNavigation(href);
}
}
});
@@ -33,6 +32,20 @@ registerFunction(`${registeredFunctionPrefix}.enableNavigationInteception`, () =
window.addEventListener('popstate', handleInternalNavigation);
});
+registerFunction(`${registeredFunctionPrefix}.navigateTo`, (uriDotNetString: System_String) => {
+ const href = platform.toJavaScriptString(uriDotNetString);
+ if (isWithinBaseUriSpace(toAbsoluteUri(href))) {
+ performInternalNavigation(href);
+ } else {
+ location.href = href;
+ }
+});
+
+function performInternalNavigation(href: string) {
+ history.pushState(null, /* ignored title */ '', href);
+ handleInternalNavigation();
+}
+
function handleInternalNavigation() {
if (!notifyLocationChangedMethod) {
notifyLocationChangedMethod = platform.findMethod(
diff --git a/src/Microsoft.AspNetCore.Blazor.Browser/Services/BrowserUriHelper.cs b/src/Microsoft.AspNetCore.Blazor.Browser/Services/BrowserUriHelper.cs
index 28cf8e497f..171679febb 100644
--- a/src/Microsoft.AspNetCore.Blazor.Browser/Services/BrowserUriHelper.cs
+++ b/src/Microsoft.AspNetCore.Blazor.Browser/Services/BrowserUriHelper.cs
@@ -104,6 +104,17 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services
throw new ArgumentException($"The URI '{absoluteUri}' is not contained by the base URI '{baseUriPrefix}'.");
}
+ ///
+ public void NavigateTo(string uri)
+ {
+ if (uri == null)
+ {
+ throw new ArgumentNullException(nameof(uri));
+ }
+
+ RegisteredFunction.InvokeUnmarshalled