I have a simple web app I would like to run in a WinUI app that can fetch external HTML content:
private async fetchHtml() {
const url = document.querySelector<HTMLInputElement>("#txt-url")!.value;
const html = await fetch(url).then(r => r.text());
document.querySelector<HTMLTextAreaElement>("#txt-html")!.value = html;
}
Obviously this would hit a CORS error. I want to disable it but cannot find any way for WinUI3:
WebView2 in WinUI does not have
EnsureCoreWebView2Async
overload that takesCoreWebView2EnvironmentOptions
so this answer does not help.The URL is external URL so
SetVirtualHostNameToFolderMapping
suggested by this answer does not help as well.I tried injecting my own CORS header but it does not work on
fetch
(WebResourceResponseReceived
event is not fired only onfetch
requests):
c.WebResourceResponseReceived += (_, e) =>
{
var origin = e.Request.Headers.FirstOrDefault(q => q.Key == "Origin").Value;
if (!string.IsNullOrEmpty(origin))
{
e.Response.Headers.AppendHeader("Access-Control-Allow-Origin",
new Uri(e.Request.Uri).GetLeftPart(UriPartial.Authority));
e.Response.Headers.AppendHeader("Access-Control-Allow-Methods", "*");
e.Response.Headers.AppendHeader("Access-Control-Allow-Headers", "*");
}
};