Skip to content

Instantly share code, notes, and snippets.

@flashlin
Created July 1, 2021 14:30
Show Gist options
  • Save flashlin/c50b46a2181682cf4aa2a712d2de2aff to your computer and use it in GitHub Desktop.
Save flashlin/c50b46a2181682cf4aa2a712d2de2aff to your computer and use it in GitHub Desktop.
Blazor Starter
// 如何在 WPF WebView2 中禁用 CORS ?
// 在 WebView2.Source 中指定 URL 之前需要設置。注意不要在 Xaml 端指定 Source
/// <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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment