Search code examples
c#webview2winui-3

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


Solution

  • 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.