使用 ASP..NET Core REST API 服务的 Uno WebAssembly 上的 CORS 错误

CORS Error on Uno WebAssembly with ASP..NET Core REST API Service

我在使用 Uno 平台的 WebAssembly 上遇到了这些错误。

Access to fetch at 'https://localhost:44318/api/search/bebek/TR' from origin 'http://localhost:49917' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

我从 API 服务获得了一些数据响应作为 JSON 文件。 UWP 应用程序没有错误:

 //_savedSearchList = await _dbService.SearchAsync(_keyword, _sentLanguageArgument);  // Normal database connection for UWP.

                //_savedSearchList = await _dbService.SearchAsync(_keyword, _sentLanguageArgument);  // Normal database connection for UWP.

                //Get search list for webservice.
                var link_search = $"https://localhost:44318/api/search/{_keyword.ToLower()}/{_sentLanguageArgument}";



                using (HttpClient client = new HttpClient())
                {
                    HttpResponseMessage response = await client.GetAsync(link_search);
                    Debug.WriteLine($"Http Status Code for Connection: {response.StatusCode}");
                    if (response.IsSuccessStatusCode)
                    {
                        var jsonString = await response.Content.ReadAsStringAsync();
                        _savedSearchList = JsonConvert.DeserializeObject<List<SearchResultCapsule>>(jsonString).OrderBy(t => t.IssueNumber);



                        if (_savedSearchList.Count() != 0)
                        {

                            ResultList.ItemsSource = _savedSearchList;
                            NoResult_Grid.Visibility = Visibility.Collapsed;
                        }
                    }
                }

WebAssembly 的真正问题是什么?我该如何解决?谢谢

这是 Javascript fetch API 的安全限制,您调用的端点需要提供 CORS headers 才能正常工作。

如果您控制 API,则需要使用框架中的功能来启用 CORS,如果不这样做,则需要请求端点的维护者启用 CORS .

要测试 CORS 是否真的是问题所在,您可以使用 CORS Anywhere 代理查询。

通过Microsoft CORS包解决。谢谢。