Another option is to create a virtual host. For a detailed information see CoreWebView2.SetVirtualHostNameToFolderMapping
:
Sets a mapping between a virtual host name and a folder path to make available to web sites via that host name.
Example:
public class VirtualHost
{
private WebView2 webView;
private string hostName;
private string directory;
public VirtualHost(WebView2 webView, string hostName, string directory)
{
this.webView = webView;
this.hostName = hostName;
this.directory = directory;
_ = InitializeAsync();
}
async private Task InitializeAsync()
{
// Wait for CoreWebView2 initialization.
// Explicitly triggers initialization of the control's CoreWebView2.
await webView.EnsureCoreWebView2Async();
// Set a mapping between a virtual host name and a folder path
// to make available to web sites via that host name.
webView.CoreWebView2.SetVirtualHostNameToFolderMapping(
hostName, directory, CoreWebView2HostResourceAccessKind.DenyCors);
webView.Source = new Uri($"https://{hostName}/index.html");
}
}
var localPath = Environment.GetEnvironmentVariable("LocalAppData");
virtualHost = new VirtualHost(view.configuratorWebView, "mylocalhost", localPath);