--- title: ASP.NET Core Blazor Hybrid routing and navigation author: guardrex description: Learn how to manage request routing and navigation in Blazor Hybrid apps. monikerRange: '>= aspnetcore-6.0' ms.author: riande ms.custom: "mvc" ms.date: 11/12/2024 uid: blazor/hybrid/routing zone_pivot_groups: blazor-hybrid-frameworks --- # ASP.NET Core Blazor Hybrid routing and navigation [!INCLUDE[](~/includes/not-latest-version.md)] This article explains how to manage request routing and navigation in Blazor Hybrid apps. ## URI request routing behavior Default URI request routing behavior: * A link is *internal* if the host name and scheme match between the app's origin URI and the request URI. When the host names and schemes don't match or if the link sets `target="_blank"`, the link is considered *external*. * If the link is internal, the link is opened in the `BlazorWebView` by the app. * If the link is external, the link is opened by an app determined by the device based on the device's registered handler for the link's scheme. * For internal links that appear to request a file because the last segment of the URI uses dot notation (for example, `/file.x`, `/Maryia.Melnyk`, `/image.gif`) but don't point to any static content: * WPF and Windows Forms: The host page content is returned. * .NET MAUI: A 404 response is returned. To change the link handling behavior for links that don't set `target="_blank"`, register the `UrlLoading` event and set the property. The enumeration allows setting link handling behavior to any of the following values: * : Load the URL using an app determined by the device. This is the default strategy for URIs with an external host. * : Load the URL within the `BlazorWebView`. This is the default strategy for URLs with a host matching the app origin. ***Don't use this strategy for external links unless you can ensure the destination URI is fully trusted.*** * : Cancels the current URL loading attempt. The property is used to get or dynamically set the URL. > [!WARNING] > External links are opened in an app determined by the device. Opening external links within a `BlazorWebView` can introduce security vulnerabilities and shouldn't be enabled unless you can ensure that the external links are fully trusted. API documentation: * .NET MAUI: * WPF: * Windows Forms: The namespace is required for the following examples: ```csharp using Microsoft.AspNetCore.Components.WebView; ``` :::zone pivot="maui" Add the following event handler to the constructor of the `Page` where the `BlazorWebView` is created, which is `MainPage.xaml.cs` in an app created from the .NET MAUI project template. ```csharp blazorWebView.UrlLoading += (sender, urlLoadingEventArgs) => { if (urlLoadingEventArgs.Url.Host != "0.0.0.0") { urlLoadingEventArgs.UrlLoadingStrategy = UrlLoadingStrategy.OpenInWebView; } }; ``` :::zone-end :::zone pivot="wpf" Add the `UrlLoading="Handle_UrlLoading"` attribute to the `BlazorWebView` control in the `.xaml` file: ```xaml ``` Add the event handler in the `.xaml.cs` file: ```csharp private void Handle_UrlLoading(object sender, UrlLoadingEventArgs urlLoadingEventArgs) { if (urlLoadingEventArgs.Url.Host != "0.0.0.0") { urlLoadingEventArgs.UrlLoadingStrategy = UrlLoadingStrategy.OpenInWebView; } } ``` :::zone-end :::zone pivot="winforms" In the constructor of the form containing the `BlazorWebView` control, add the following event registration: ```csharp blazorWebView.UrlLoading += (sender, urlLoadingEventArgs) => { if (urlLoadingEventArgs.Url.Host != "0.0.0.0") { urlLoadingEventArgs.UrlLoadingStrategy = UrlLoadingStrategy.OpenInWebView; } }; ``` :::zone-end :::moniker range=">= aspnetcore-8.0" ## Get or set a path for initial navigation Use the `BlazorWebView.StartPath` property to get or set the path for initial navigation within the Blazor navigation context when the Razor component is finished loading. The default start path is the relative root URL path (`/`). :::zone pivot="maui" In the `MainPage` XAML markup (`MainPage.xaml`), specify the start path. The following example sets the path to a welcome page at `/welcome`: ```xaml ... ``` Alternatively, the start path can be set in the `MainPage` constructor (`MainPage.xaml.cs`): ```csharp blazorWebView.StartPath = "/welcome"; ``` :::zone-end :::zone pivot="wpf" In the `MainWindow` designer (`MainWindow.xaml`), specify the start path. The following example sets the path to a welcome page at `/welcome`: ```xaml ... ``` :::zone-end :::zone pivot="winforms" Inside the `Form1` constructor of the `Form1.cs` file, specify the start path. The following example sets the path to a welcome page at `/welcome`: ```csharp blazorWebView1.StartPath = "/welcome"; ``` :::zone-end :::moniker-end :::zone pivot="maui" ## Navigation among pages and Razor components This section explains how to navigate among .NET MAUI content pages and Razor components. The .NET MAUI Blazor hybrid project template isn't a [Shell-based app](/dotnet/maui/fundamentals/shell/), so the [URI-based navigation for Shell-based apps](/dotnet/maui/fundamentals/shell/navigation) isn't suitable for a project based on the project template. The examples in this section use a to perform modeless or modal navigation. In the following example: * The namespace of the app is `MauiBlazor`, which matches the suggested project name of the tutorial. * A is placed in a new folder added to the app named `Views`. In `App.xaml.cs`, create the `MainPage` as a by making the following change: ```diff - MainPage = new MainPage(); + MainPage = new NavigationPage(new MainPage()); ``` `Views/NavigationExample.xaml`: ```xaml