CoreWebView2.NavigateToString(String) method fails to load an HTML page

AdiGeo 20 Reputation points
2024-10-31T17:08:20.23+00:00

I am using an instance of WebView2 class to display some HTML content in a UI panel for a VS 2022 extension. I can retrieve the HTML content that I want to display in the WebView2 control as a string, and then I am trying to use myWebView2.CoreWebView2.NavigateToString(myHTMLPage) method to have the retrieved page displayed. The HTML content has a lot of JavaScript code in it, and while loading I am getting the following exception:

Uncaught DOMException: Failed to read the 'sessionStorage' property from 'Window': Access is denied for this document.

The JavaScript code needs to access the Window.sessionStorage, but I found that the value is undefined.

As a workaround, I save the HTML content in a temporary HTML file in the file system, and I use myWebView2.CoreWebView2.Navigate(myHTMLfile). This time the page loads and displays successfully. I delete the file once the page has loaded.

I wonder if when using the NavigateToString() method is there any solution to successfully display the page? I want to avoid creating a temporary file.

Thank you

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,225 questions
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
798 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,666 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,054 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 118.4K Reputation points
    2024-10-31T20:25:37.1366667+00:00

    If the WebView is initialized, the next experimental button handler shows the usage of WebResourceRequested event, which allows to deliver the local contents:

    private void button1_Click( object sender, EventArgs e )
    {
        webView.CoreWebView2.AddWebResourceRequestedFilter( "file://demo/*", CoreWebView2WebResourceContext.All );
    
        webView.CoreWebView2.WebResourceRequested += ( s, a ) =>
        {
            if( a.Request.Uri == "file://demo/test1.html" )
            {
                string html = @"<html>
    <head>
    <script>
    function OnLoad()
    {
       try 
       {                        
          alert( window.sessionStorage);
          alert( window.sessionStorage.getItem('myKey') );
          window.sessionStorage.setItem('myKey', Date.now() );
       } 
       catch(error) 
       {
          alert(error);
       }
    }
    </script>
    </head>
    <body onload=""OnLoad()"">
    <h1>TEST</h1>
    <p>
    Hello!
    </p>
    </body>
    </html>";
    
                var ms = new MemoryStream( Encoding.UTF8.GetBytes( html ) );
                a.Response = webView.CoreWebView2.Environment.CreateWebResourceResponse( ms, 200, "OK", "Content-Type: text/html" );
            }
        };
    
        webView.CoreWebView2.Navigate( "file://demo/test1.html" );
    }
    

    Accessing the window.sessionStorage object in this sample should not fail. Maybe it will work in your tests too.

    For specific details and optimizations, see:

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AdiGeo 20 Reputation points
    2024-10-31T21:01:02.0833333+00:00

    I just tried the solution you proposed, and works perfect.

    Many thanks,


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.