Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor Hybrid routing and nav topic #25130

Merged
merged 4 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions aspnetcore/blazor/hybrid/routing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
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: 02/24/2022
no-loc: ["Blazor Hybrid", Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
uid: blazor/hybrid/routing
---
# ASP.NET Core Blazor Hybrid routing and navigation

[!INCLUDE[](~/blazor/includes/blazor-hybrid-preview-notice.md)]

Register to the `ExternalNavigationStarting` event and set the `ExternalLinkNavigationEventArgs.ExternalLinkNavigationPolicy` property to change link handling behavior. The `ExternalLinkNavigationPolicy` enumeration (`enum`) allows setting link handling behavior to `OpenInExternalBrowser`, `InsecureOpenInWebView`, and `CancelNavigation`. The `ExternalLinkNavigationEventArgs.Uri` property can be used to dynamically set link handling behavior.

> [!WARNING]
> External links are opened in the device's default browser by default. Opening external links within a `BlazorWebView` can introduce security vulnerabilities and should ***not*** be enabled unless you can ensure that the external links are fully trusted.

## .NET MAUI

For the `Page` with the `BlazorWebView`:

* Add the `Microsoft.AspNetCore.Components.WebView` namespace:

```csharp
using Microsoft.AspNetCore.Components.WebView;
```
guardrex marked this conversation as resolved.
Show resolved Hide resolved

* Add the following event handler to the constructor:

```csharp
blazorWebView.ExternalNavigationStarting +=
(sender, externalLinkNavigationEventArgs) =>
{
externalLinkNavigationEventArgs.ExternalLinkNavigationPolicy =
ExternalLinkNavigationPolicy.InsecureOpenInWebView;
};
```

## WPF

Add the `ExternalNavigationStarting="Handle_ExternalNavigationStarting"` attribute to the `BlazorWebView` control in the `.xaml` file:

```xaml
<blazor:BlazorWebView HostPage="wwwroot\index.html"
Services="{StaticResource services}"
x:Name="blazorWebView"
ExternalNavigationStarting="Handle_ExternalNavigationStarting" >
```

Add the event handler in the `.xaml.cs` file:

```csharp
private void Handle_ExternalNavigationStarting(
object sender, ExternalLinkNavigationEventArgs externalLinkNavigationEventArgs)
{
externalLinkNavigationEventArgs.ExternalLinkNavigationPolicy =
ExternalLinkNavigationPolicy.InsecureOpenInWebView;
}
```

## Winforms

In the constructor of the form containing the `BlazorWebView` control, add the following event registration:

```csharp
blazorWebView.ExternalNavigationStarting +=
(sender, externalLinkNavigationEventArgs) =>
{
externalLinkNavigationEventArgs.ExternalLinkNavigationPolicy =
ExternalLinkNavigationPolicy.InsecureOpenInWebView;
};
```
2 changes: 2 additions & 0 deletions aspnetcore/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ items:
uid: blazor/hybrid/tutorials/windows-forms
- name: WPF
uid: blazor/hybrid/tutorials/wpf
- name: Routing and navigation
uid: blazor/hybrid/routing
- name: Project structure
uid: blazor/project-structure
- name: Fundamentals
Expand Down