MapVirtualHost Method  
 

Maps a virtual host name to a folder path, making it available using that name.

Syntax

object.MapVirtualHost( HostName, FolderPath, [ AccessKind ] )

Parameters

HostName
A string which specifies the virtual host name to associate with the local directory. The name must be a valid hostname and must not include a URL scheme, port number, or path. This parameter cannot be an empty string.
FolderPath
A string which specifies the path to the local directory. Environment variables in the path are expanded, and relative paths are converted to absolute paths. The directory must exist and be accessible to the current process. This parameter cannot be an empty string.
AccessKind
An optional integer value which specifies the level of access that other sites have to the resources in the folder. If this parameter is not specified, it will default to webHostAccessDeny.
Value Description
webHostAccessDeny All cross origin resource access is denied.
webHostAccessAllow All cross origin resource access is allowed, including accesses that are subject to Cross-Origin Resource Sharing (CORS) checks.
webHostAccessDenyCrossOrigin Cross origin resource access is allowed for normal resources, such as the source of a script or image element. Access that requires a CORS check will be denied.

Return Value

Returns True if the host name could be mapped to the local folder. If the host name is invalid or the local path does not exist, the method returns False.

Remarks

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.

Example

' Map a local directory to a virtual HTTPS host
If WebView1.MapVirtualHost("appassets.example", "C:\MyApplication\WebContent") Then
    ' Load the local HTML document
    WebView1.Navigate "https://appassets.example/index.html"
End If

See Also