Hello,
Welcome to Microsoft Q&A!
How can I view HTML files in local on winUI as webView 2?
Using ms-appx-web
directly to load local content in WebView2 is not supported. If you need to load local HTML content, please do it in code-behind.
You could try to use the following code:
var storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/html/TestWeb.html"));
MyWebView.Source = new Uri(storageFile.Path);
Another way is to use SetVirtualHostNameToFolderMapping
await MyWebView.EnsureCoreWebView2Async(); // ensure the CoreWebView2 is created
var core_wv2 = MyWebView.CoreWebView2;
if (core_wv2 != null)
{
core_wv2.SetVirtualHostNameToFolderMapping(
"appassets.example", "assets/html",
Microsoft.Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow);
MyWebView.Source = new Uri(@"https://appassets.example/TestWeb.html");
}
You could also check this Github Hub issue for more details: https://github.com/microsoft/microsoft-ui-xaml/issues/1967
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.