I'm developing an app that displays WebView2 using .Net5/WPF.
I want to disable CORS.
I have installed Chromium Edge Canary 86.0.607.0.
In Edge, CORS could be disabled by adding the following argument to the shortcut.
--disable-web-security --user-data-dir="C://Chrome dev session"
I want to disable WebView2 as well.
I got an answer in this forum. Sorry, this site is in Japanese.
https://social.msdn.microsoft.com/Forums/ja-JP/9bac6d51-94eb-42c8-96f0-4081e9a4056d/wpf-webview2-cors-?forum=wpfja
It worked with the code below.
*It is necessary to set before specifying the URL in WebView2.Source.
Be careful not to specify Source on Xaml side.
/// <summary>
/// WPF Window Constructor
/// </summary>
/// <param name="url">URL</param>
public WebViewWindow(string url)
{
InitializeComponent();
// async start
Loaded += async (_, __) => await _Routine(url);
}
/// <summary>
/// task
/// </summary>
/// <param name="url">URL</param>
/// <returns>task</returns>
async Task _Routine(string url)
{
var localPath = Environment.GetEnvironmentVariable("LocalAppData");
var webviewPath = $@"{localPath}\Microsoft\Edge SxS\Application\86.0.608.0";
var userPath = "C://Chrome dev session";
// Argument setting to disable COAR
var op = new CoreWebView2EnvironmentOptions("--disable-web-security");
var env = await CoreWebView2Environment.CreateAsync(webviewPath, userPath, op);
await webView.EnsureCoreWebView2Async(env);
if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out Uri? result))
{
webView.Source = result;
}
}