# Hyperlink Support

The [.NET MAUI RichTextEditor](https://www.telerik.com/maui-ui/documentation/controls/richtexteditor/overview) provides built-in support for creating and managing hyperlinks. Through the exposed commands related to hyperlinks, namely `ApplyHyperlinkCommand`, `RemoveHyperlinkCommand`, and `OpenHyperlinkCommand`, users can manipulate the hyperlinks inside the RichTextEditor content.

In addition, `RichTextEditorToolbar` exposes predefined toolbar items wired to the hyperlink commands.

* `RichTextEditorAddHyperlinkToolbarItem`—Opens a popup to enter URL information and executes an action that adds a hyperlink for the current selection.
* `RichTextEditorAddOrEditHyperlinkToolbarItem`—Opens a popup to enter URL information and executes an action that adds or edits a hyperlink for the current selection.
* `RichTextEditorHyperlinkNavigationToolbarItem`—This toolbar item adds or edits hyperlinks depending whether a hyperlink is selected. If a hyperlink is selected, the editor navigates to toolbar sub-items for operations related to a hyperlink. If a hyperlink is not selected, a popup with a dialog is displayed to enter URL information and add a hyperlink for the current selection.

## Additional Toolbar Items for Hyperlink Operations

The `RichTextEditorHyperlinkNavigationToolbarItem` has the following items:

* `RichTextEditorEditHyperlinkToolbarItem`—Edits the hyperlink from the current selection.
* `RichTextEditorRemoveHyperlinkToolbarItem`—Removes the hyperlink from the current selection.
* `RichTextEditorOpenHyperlinkToolbarItem`—Navigates to the URL.

  ![.NET MAUI RichTextEditor Hyperlink Navigation](https://www.telerik.com/maui-ui/documentation/assets/33daf3a7b36f41b843248bd519fdfb0f/rte-hyperlink-toolbar.png)

The next image shows the `RichTextEditorEditHyperlinkToolbarItem` on mobile platforms:

![.NET MAUI RichTextEditor Hyperlink Navigation](https://www.telerik.com/maui-ui/documentation/assets/15fc8d2264346a9fedea3e96bd9fe59f/rte-hyperlink-edit.png)

For more details, see the [RichTextEditor Toolbar](https://www.telerik.com/maui-ui/documentation/controls/richtexteditor/toolbar/overview#predefined-toolbar-items) topic.

## Methods

You can also take advantage of the following API related to hyperlinks:

* `GetHyperlinkAsync` method—Asynchronously returns a `RichTextHyperlink` under the caret in the editor (or `null` if no hyperlink is present). The `RichTextHyperlink` object contains the `Url` and `Title` of the link.

## Hyperlink Error Handling

In case users try to open invalid URLs (for example, the URL is not absolute, the domain does not exist or is incomplete, and so on), the following message is shown by default and indicates that the URL has an error:

![.NET MAUI RichTextEditor Hyperlink Error Handling](https://www.telerik.com/maui-ui/documentation/assets/255783466fb4f62065eb5b202a536e57/rte-hyperlink-error-handling.png)

You can override the default behavior by handling the RichTextEditor's `OpenHyperlinkError` event:

* `OpenHyperlinkError` event—Raised when users try to open invalid URLs in the editor. The `OpenHyperlinkError` event handler receives two parameters:

  * The `Sender` which is the RichTextEditor control.
  * `OpenHyperlinkErrorEventArgs` provides the following properties:
    * `Error`—of type `System.Exception`, can be used to get the exact error message.
    * `Url`—of type `string`, defining the URL of the hyperlink.
    * `Handled`—boolean property indicating whether the event is handled.

To change the default error handling behavior:

1. Subscribe to the event.
2. Set the `Handled` property of the event arguments to `True` to prevent the default message.
3. Add a custom implementation.

The following example demonstrates how to use the `OpenHyperlinkError` event:

```XAML
<telerik:RadRichTextEditor x:Name="richTextEditor" 
                           OpenHyperlinkError="RichTextEditor_OpenHyperlinkError"
                           Grid.Row="{OnIdiom Desktop=1, Phone=0}" />
```

The next snippet shows the event handler which shows a custom message:

```C#
private void RichTextEditor_OpenHyperlinkError(object sender, OpenHyperlinkErrorEventArgs e)
{
    e.Handled = true;
    Application.Current.Windows[0].Page.DisplayAlert(string.Format("Error opening {0}", e.Url), e.Error.Message, "Ok");
}
```

> For a runnable example with the RichTextEditor Hyperlink Error Handling, see the [SDKBrowser Demo Application](https://www.telerik.com/maui-ui/documentation/demos-and-sample-apps/sdkbrowser-app) and go to **RichTextEditor > Events**.

## See Also

* [Commands](https://www.telerik.com/maui-ui/documentation/controls/richtexteditor/commands)
* [Configure the RichTextEditor](https://www.telerik.com/maui-ui/documentation/controls/richtexteditor/configuration)
* [Events](https://www.telerik.com/maui-ui/documentation/controls/richtexteditor/events)
* [Working with images](https://www.telerik.com/maui-ui/documentation/controls/richtexteditor/working-with-images/overview)
