1

From uno platform WASM client I am trying to call a asp.net core rest demo:

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

When the wasm client calls the rest I get TypeError: Failed to fetch but this works just fine UWP and console apps.

Client is using

 public async Task<string> RefreshDataAsync(string x)
        {
            var _client = new HttpClient();


            var response = await _client.GetStringAsync(@"http://localhost:58658/weatherforecast");
            return response;


        }

Does WASM uno platform support rest api calls?

4

2 回答 2

4

原来需要其余的api

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseCors(builder => builder
   .AllowAnyOrigin()
   .AllowAnyMethod()
   .AllowAnyHeader()
   );
于 2020-03-18T18:43:37.833 回答
3

Uno 平台中 Web 服务的使用(假设为 http/json)与任何 .NET 应用程序的工作方式相同。使用HttpClient

对于 WebAssembly,您需要创建一个 WasmHttpHandler,然后将其作为 HttpClient 的 innerHandler 传入。

#if __WASM__
            var innerHandler = new Uno.UI.Wasm.WasmHttpHandler();
#else
            var innerHandler = new HttpClientHandler();
#endif
            _httpClient = new HttpClient(innerHandler);

有关使用 HttpClient 的示例,请参阅https://github.com/unoplatform/uado 。

于 2020-03-18T00:00:39.727 回答