I'm building an application and want to use HTML, CSS, & JS files as embedded resources but still load/reference them in a WebView2 component. Is there any good (and efficient) way to do this?
I've looked at SetVirtualHostNameToFolderMapping but I don't think that works for embedded resources.
With WebView2, the browser runs in a separate process, and only the rendered output appears in your window.
All the major browsers have moved in that direction lately, not only for hosted browsers but for the ordinary end-user experience. Process separation improves stability and protects against information leakage/corruption that might otherwise result from a variety of attacks such as RowHammer and Spectre vulnerabilities.
Actually, a single page can be implemented as a group of processes, to isolate scripts loaded from different domains and prevent XSS.
By design the browser processes cannot directly access data in your process. So you'll have to hook into its network request events.
For more information read
In particular, note that
Another way you can host local content in a
WebView2
control is by relying on theWebResourceRequested
event. This event is triggered when the control attempts to load a resource. You can use this event to intercept the request and provide the local content, as described in Custom management of network requests.
There is an example named Overriding a response, to proactively replace it that allows you to use a Stream
object to return content to the WebView2.