From 074aeae1fd7a5e304c625527dce82c17d666cfb9 Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Fri, 2 Sep 2022 04:08:36 -0500 Subject: [PATCH 1/2] Blazor Hybrid routing (external nav, pivots) --- aspnetcore/blazor/hybrid/routing.md | 110 ++++++++++++++++++++++++---- 1 file changed, 97 insertions(+), 13 deletions(-) diff --git a/aspnetcore/blazor/hybrid/routing.md b/aspnetcore/blazor/hybrid/routing.md index 86e72edbab29..19d2801eec90 100644 --- a/aspnetcore/blazor/hybrid/routing.md +++ b/aspnetcore/blazor/hybrid/routing.md @@ -7,6 +7,7 @@ ms.author: riande ms.custom: "mvc" ms.date: 04/14/2022 uid: blazor/hybrid/routing +zone_pivot_groups: blazor-hybrid-frameworks --- # ASP.NET Core Blazor Hybrid routing and navigation @@ -41,8 +42,8 @@ The name ```csharp using Microsoft.AspNetCore.Components.WebView; ``` - -## .NET MAUI + +:::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. The following example assumes an `x:Name="blazorWebView"` ([`x:Name` directive](/dotnet/desktop/xaml-services/xname-directive)) on the `BlazorWebView` within the `.xaml` file. @@ -57,7 +58,9 @@ blazorWebView.UrlLoading += }; ``` -## WPF +:::zone-end + +:::zone pivot="wpf" Add the `UrlLoading="Handle_UrlLoading"` attribute to the `BlazorWebView` control in the `.xaml` file: @@ -65,7 +68,7 @@ Add the `UrlLoading="Handle_UrlLoading"` attribute to the `BlazorWebView` contro + UrlLoading="Handle_UrlLoading"> ``` Add the event handler in the `.xaml.cs` file: @@ -81,7 +84,9 @@ private void Handle_UrlLoading(object sender, } ``` -## Windows Forms +:::zone-end + +:::zone pivot="winforms" In the constructor of the form containing the `BlazorWebView` control, add the following event registration: @@ -96,6 +101,8 @@ blazorWebView.UrlLoading += }; ``` +:::zone-end + :::moniker-end :::moniker range=">= aspnetcore-7.0" @@ -127,8 +134,10 @@ The name ```csharp using Microsoft.AspNetCore.Components.WebView; ``` - -## .NET MAUI + +## Internal navigation + +:::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. @@ -138,12 +147,15 @@ blazorWebView.UrlLoading += { if (urlLoadingEventArgs.Url.Host != "0.0.0.0") { - urlLoadingEventArgs.UrlLoadingStrategy = UrlLoadingStrategy.OpenInWebView; + urlLoadingEventArgs.UrlLoadingStrategy = + UrlLoadingStrategy.OpenInWebView; } }; ``` -## WPF +:::zone-end + +:::zone pivot="wpf" Add the `UrlLoading="Handle_UrlLoading"` attribute to the `BlazorWebView` control in the `.xaml` file: @@ -151,7 +163,7 @@ Add the `UrlLoading="Handle_UrlLoading"` attribute to the `BlazorWebView` contro + UrlLoading="Handle_UrlLoading"> ``` Add the event handler in the `.xaml.cs` file: @@ -167,7 +179,9 @@ private void Handle_UrlLoading(object sender, } ``` -## Windows Forms +:::zone-end + +:::zone pivot="winforms" In the constructor of the form containing the `BlazorWebView` control, add the following event registration: @@ -177,11 +191,81 @@ blazorWebView.UrlLoading += { if (urlLoadingEventArgs.Url.Host != "0.0.0.0") { - urlLoadingEventArgs.UrlLoadingStrategy = UrlLoadingStrategy.OpenInWebView; + urlLoadingEventArgs.UrlLoadingStrategy = + UrlLoadingStrategy.OpenInWebView; } }; ``` -:::moniker-end +:::zone-end + +## External navigation + +Register to the `ExternalNavigationStarting` event and set the `ExternalLinkNavigationEventArgs.ExternalLinkNavigationPolicy` property to change navigation behavior. + +The `ExternalLinkNavigationPolicy` enum sets the navigation behavior: + +* `OpenInExternalBrowser`: Allows navigation to external links using the system default browser. This is the default navigation policy. +* `InsecureOpenInWebView`: Allows navigation to external links within the Blazor WebView. This navigation policy can introduce security concerns and shouldn't be enabled unless you can ensure that all external links are fully trusted. +* `CancelNavigation`: Cancels the current navigation attempt to an external link. + +The `ExternalLinkNavigationEventArgs.Uri` property contains the destination URI. + +> [!WARNING] +> By default, external links are opened in the device default browser. Opening external links within the Blazor WebView (`InsecureOpenInWebView`) is ***not recommended*** unless the content is fully trusted. + +:::zone pivot="maui" + +Add the event handler to the constructor of the page where you construct the `BlazorWebView`: + +```csharp +blazorWebView.ExternalNavigationStarting += + (sender, externalLinkNavigationEventArgs) => + { + externalLinkNavigationEventArgs.ExternalLinkNavigationPolicy = + ExternalLinkNavigationPolicy.OpenInExternalBrowser; + }; +``` + +:::zone-end +:::zone pivot="wpf" +Add the `ExternalNavigationStarting="Handle_ExternalNavigationStarting"` attribute to the `BlazorWebView` control in the `.xaml` file: + +```xaml + +``` + +Add the event handler in the `.xaml.cs` file: + +```csharp +private void Handle_ExternalNavigationStarting(object sender, + ExternalLinkNavigationEventArgs externalLinkNavigationEventArgs) +{ + externalLinkNavigationEventArgs.ExternalLinkNavigationPolicy = + ExternalLinkNavigationPolicy.OpenInExternalBrowser; +} +``` + +:::zone-end + +:::zone pivot="winforms" + +In the contructor of the form containing the `BlazorWebView` control, add the following event registration: + +```csharp +blazorWebView.ExternalNavigationStarting += + (sender, externalLinkNavigationEventArgs) => + { + externalLinkNavigationEventArgs.ExternalLinkNavigationPolicy = + ExternalLinkNavigationPolicy.OpenInExternalBrowser; + }; +``` + +:::zone-end + +:::moniker-end From 2ec4f855e7706fc7e5dec434e6c35d9bc64a6bfc Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Fri, 2 Sep 2022 04:15:44 -0500 Subject: [PATCH 2/2] Updates --- aspnetcore/blazor/hybrid/routing.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/aspnetcore/blazor/hybrid/routing.md b/aspnetcore/blazor/hybrid/routing.md index 19d2801eec90..71019368167a 100644 --- a/aspnetcore/blazor/hybrid/routing.md +++ b/aspnetcore/blazor/hybrid/routing.md @@ -203,20 +203,20 @@ blazorWebView.UrlLoading += Register to the `ExternalNavigationStarting` event and set the `ExternalLinkNavigationEventArgs.ExternalLinkNavigationPolicy` property to change navigation behavior. -The `ExternalLinkNavigationPolicy` enum sets the navigation behavior: +The `ExternalLinkNavigationPolicy` enumeration sets the navigation behavior: -* `OpenInExternalBrowser`: Allows navigation to external links using the system default browser. This is the default navigation policy. -* `InsecureOpenInWebView`: Allows navigation to external links within the Blazor WebView. This navigation policy can introduce security concerns and shouldn't be enabled unless you can ensure that all external links are fully trusted. -* `CancelNavigation`: Cancels the current navigation attempt to an external link. +* `OpenInExternalBrowser`: Navigate to external links using the device's default browser. This is the default navigation policy. +* `InsecureOpenInWebView`: Navigate to external links within the Blazor WebView. This navigation policy can introduce security concerns and shouldn't be enabled unless you can ensure that all external links are fully trusted. +* `CancelNavigation`: Cancels the current navigation attempt. The `ExternalLinkNavigationEventArgs.Uri` property contains the destination URI. > [!WARNING] -> By default, external links are opened in the device default browser. Opening external links within the Blazor WebView (`InsecureOpenInWebView`) is ***not recommended*** unless the content is fully trusted. +> By default, external links are opened in the device's default browser. Opening external links within the Blazor WebView (`InsecureOpenInWebView`) is ***not recommended*** unless the content is fully trusted. :::zone pivot="maui" -Add the event handler to the constructor of the page where you construct the `BlazorWebView`: +Add the event handler to the constructor of the page where the `BlazorWebView` is constructed: ```csharp blazorWebView.ExternalNavigationStarting +=