Skip to content

Commit afb2939

Browse files
committed
Fix for dotnet#5523 MauiWebView not loading local files on Windows
1 parent 7d5daaf commit afb2939

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/Core/src/Platform/Windows/MauiWebView.cs

+28-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
using System.Diagnostics;
33
using System.Text.RegularExpressions;
44
using Microsoft.UI.Xaml.Controls;
5+
using Windows.ApplicationModel;
56

67
namespace Microsoft.Maui.Platform
78
{
89
public class MauiWebView : WebView2, IWebViewDelegate
910
{
1011
WebView2? _internalWebView;
1112

12-
const string LocalScheme = "ms-appx-web:///";
13+
// Arbitrary local host name for virtual folder mapping
14+
const string LocalHostName = "appxpackage";
15+
const string LocalScheme = $"https://{LocalHostName}/";
1316

1417
// Script to insert a <base> tag into an HTML document
1518
const string BaseInsertionScript = @"
@@ -21,9 +24,12 @@ public class MauiWebView : WebView2, IWebViewDelegate
2124

2225
public async void LoadHtml(string? html, string? baseUrl)
2326
{
27+
var mapAppxFolder = false;
28+
2429
if (string.IsNullOrEmpty(baseUrl))
2530
{
2631
baseUrl = LocalScheme;
32+
mapAppxFolder = true;
2733
}
2834

2935
// Generate a base tag for the document
@@ -36,7 +42,7 @@ public async void LoadHtml(string? html, string? baseUrl)
3642
_internalWebView = new WebView2();
3743

3844
// TODO: For now, the CoreWebView2 won't be created without either setting Source or
39-
// calling EnsureCoreWebView2Async().
45+
// calling EnsureCoreWebView2Async().
4046
await _internalWebView.EnsureCoreWebView2Async();
4147

4248
// When the 'navigation' to the original HTML string is done, we can modify it to include our <base> tag
@@ -55,6 +61,16 @@ public async void LoadHtml(string? html, string? baseUrl)
5561

5662
await EnsureCoreWebView2Async();
5763

64+
if (mapAppxFolder)
65+
{
66+
var appxFolder = Package.Current.InstalledLocation.Path;
67+
68+
CoreWebView2.SetVirtualHostNameToFolderMapping(
69+
LocalHostName,
70+
appxFolder,
71+
Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow);
72+
}
73+
5874
// Set the HTML for the 'real' WebView to the updated HTML
5975
NavigateToString(!string.IsNullOrEmpty(htmlWithBaseTag) ? htmlWithBaseTag : html);
6076

@@ -66,12 +82,21 @@ public async void LoadHtml(string? html, string? baseUrl)
6682
_internalWebView.NavigateToString(html);
6783
}
6884

69-
public void LoadUrl(string? url)
85+
public async void LoadUrl(string? url)
7086
{
7187
Uri uri = new Uri(url ?? string.Empty, UriKind.RelativeOrAbsolute);
7288

7389
if (!uri.IsAbsoluteUri)
7490
{
91+
await EnsureCoreWebView2Async();
92+
93+
var appxFolder = Package.Current.InstalledLocation.Path;
94+
95+
CoreWebView2.SetVirtualHostNameToFolderMapping(
96+
LocalHostName,
97+
appxFolder,
98+
Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow);
99+
75100
uri = new Uri(LocalScheme + url, UriKind.RelativeOrAbsolute);
76101
}
77102

0 commit comments

Comments
 (0)