The MapVirtualHost method allows applications
to make local HTML documents, scripts, stylesheets, images, and other
resources available through a virtual HTTPS host name. This is useful
for applications that include web-based user interfaces or locally
stored content, but need to avoid the security and origin restrictions
associated with loading files directly using the file://
URL scheme.
The virtual host name is associated with a directory on the
local file system. When the browser navigates to a URL using that host
name, it retrieves the corresponding file from the mapped
directory without requiring a web server or network connection. For
example, mapping the host name appassets.example to
C:\MyApplication\WebContent allows the application to
navigate to https://appassets.example/index.html to
load the file C:\MyApplication\WebContent\index.html.
Relative references to scripts, stylesheets, images, and other resources
are resolved using the same virtual host.
Although resources in the mapped directory are accessed using an
https:// URL, they are loaded directly from the local file
system. No network connection is established, and TLS encryption or
certificate validation is not performed. Content loaded through a
virtual host has a normal HTTPS origin, allowing it to use browser
features such as local storage and other APIs that may be restricted
for local files.
When a virtual host name is mapped to a local folder, the
WebView2 runtime serves files from that folder using the virtual host
name specified in the URL. Unlike a conventional web server, WebView2
does not automatically resolve requests for a directory to a default
document, such as index.html or default.html.
Applications must specify the complete path to the file being
requested. For example, the URL
https://appassets.example/index.html can be used to load
that document, but https://appassets.example/ will not
automatically load it.
Content loaded through a virtual host mapping may take
slightly longer to load than the same content accessed using a
file:// URL or retrieved from a conventional web server.
This is because WebView2 processes requests for mapped resources
through its internal resource-loading mechanism, which includes
resolving the virtual host name and mapping requests to files in
the local folder. The additional processing overhead can be more
noticeable when loading pages that contain numerous small resources,
such as images, stylesheets, and JavaScript files. Minor differences
in loading performance are expected and do not necessarily indicate a
problem with the virtual host mapping.
Although virtual host mappings are convenient for displaying
bundled HTML content and other local resources, they are not intended
to provide all the functionality or performance characteristics
of a conventional web server. Applications should use explicit
document paths and consider the potential performance overhead when
working with large numbers of local resources.
Changes to files in a mapped directory may not be immediately
reflected in the browser because previously loaded resources can be
cached. Applications may need to reload the document or otherwise
invalidate cached resources to ensure updated content is displayed.
A virtual host mapping applies only to the current browser control.
It does not register the host name globally or make the mapping
available to other controls, even if they share the same browser
environment or user data folder. Applications that use multiple browser
controls must establish the mapping separately for each instance
that needs access to the local content.
Applications should choose a virtual host name that does not
conflict with an existing website or other host names used by the
application. Using the same host name for unrelated content can cause
unexpected behavior because resources may share the same browser
origin, including cookies and persistent storage. A dedicated host
name under a reserved domain, such as appassets.example,
is recommended to avoid conflicts with real websites and to keep
application resources isolated.
The AccessKind parameter determines whether resources in
the mapped directory can be accessed by documents loaded from other
origins. This does not restrict access by documents loaded from the
mapped host itself. For most applications, webHostAccessDeny
is recommended because it prevents other origins from accessing the
mapped resources. Applications should avoid mapping folders
containing sensitive information or files that are not intended to
be accessible through the browser.
A virtual host mapping is associated with the current control and
remains in effect until it is removed using UnmapVirtualHost
or the browser control is destroyed. Calling MapVirtualHost
again with the same host name replaces the existing mapping. Changes
to a mapping may not be reflected in documents that have already been
loaded; applications should navigate to or reload the document after
changing a mapping.