Ray
Ray

Reputation: 1676

How can we add environment parameters to Webview2 under WinUI3?

I am trying to disable-web-security for WebView2 Runtime but there is no way to overload the EnsureCoreWebView2Async() function.

Any way we can add "--disable-web-security" to the webview2 runtime under WinUI3?

{
    CoreWebView2EnvironmentOptions environmentOptions = new CoreWebView2EnvironmentOptions() {
        AdditionalBrowserArguments = "--disable-web-security"
    };
    CoreWebView2Environment environment = await CoreWebView2Environment.CreateWithOptionsAsync("","", environmentOptions);
    await MyWebView.EnsureCoreWebView2Async(environment); #This shows error
    MyWebView.Source = new Uri(Path.Combine(Environment.CurrentDirectory, @"Html\mockup.html"));
    MyWebView.CoreWebView2.PermissionRequested += CoreWebView2_PermissionRequested;
}

Screenshot:

enter image description here

Upvotes: 2

Views: 8046

Answers (3)

Dmitriy K
Dmitriy K

Reputation: 41

More recently, custom environment support has been added to WinUI3: https://github.com/microsoft/microsoft-ui-xaml/issues/6150

Upvotes: 0

Niro
Niro

Reputation: 334

You can set additional arguments by setting an environment variable before webview2 creation. Environment.SetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS", "--disable-web-security");

Upvotes: 2

David Risney
David Risney

Reputation: 4377

There's currently no way to do this with WinUI3's WebView2 control.

The WinUI3 WebView2 control does not currently support initializing with a custom CoreWebView2Environment. The CoreWebView2Environment would be the only way to pass in a command line parameter like you are trying to do.

You may open a request to change this on the WinUI3 GitHub project.

Upvotes: 2

Related Questions