Bing Webapi 在 Blazor 中失败,但在 ASP.NET Core 中失败

问题描述 投票:0回答:1

此代码在 Web 中有效,但在 Blazor 中无效 - 请提出解决方案。我可以在控制台中看到错误。

这是正确的有效网站:

https://locationsearch.azurewebsites.net/Calc

注意:请从 MS 网站获取免费的 Bing 地图密钥。

代码失败:

Uri elevationReq = new Uri(string.Format("https://api.open-meteo.com/v1/elevation?latitude={latitude}&longitude={longitude}"));

字符串格式错误异常

查看:

@page "/"
@using Newtonsoft.Json;
@using Microsoft.AspNetCore.Components.Forms

@using System.Text;
@using System.Net.Http.Headers;

<h1>Counter</h1>

<EditForm Model="@submitActivity" OnSubmit="@Submit">
<div class="row">
    <div class="col-md-3">
        <p>Location</p>
    </div>
    <div class="col-md-4">
        <input placeholder="Location" @bind="@loc" />
    </div>
</div>

<br />

<div>
    <button type="submit">Submit</button>
</div>

<div>
    @errmsg
</div>

<br />
    @latitude
<br />
    @longitude
 <br/>
    @elevation
 
</EditForm>

@code {
    private string loc; string errmsg = ""; string time1 = ""; 
    double latitude = 0.0; double longitude = 0.0; double elevation;

    string time2 = "";

    private ACTIVITY submitActivity { get; set; } = new();

    public class ACTIVITY
    {
        public string Dummy { get; set; }
    }

    private async Task Submit()
    {
        string key = "Au7sMtQzyQZRzuQ2krOIbZg8j2MGoHzzOJAmVym6vQjHq_BJ8a1YQGX3iCosFh8u";
        string query = loc; 

        using (HttpClient client = new HttpClient())
        {
            string timez = "";
            string final = "";
            string timezone = "";
            string checknull = "";

            string time3 = "";
            string timef = ""; 

            Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, key));

            client.BaseAddress = new Uri(geocodeRequest.ToString());

            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response1 = null;

            try
            {
                response1 = await client.GetAsync(geocodeRequest);
                string jsonstring = await response1.Content.ReadAsStringAsync();

                // Parse the JSON response to extract the address
                dynamic data1 = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstring);

                var result1 = data1.resourceSets[0].resources[0];

                latitude = result1.point.coordinates[0];
                longitude = result1.point.coordinates[1];

                Uri elevationReq = new Uri(string.Format("https://api.open-meteo.com/v1/elevation?latitude={latitude}&longitude={longitude}"));
                client.BaseAddress = new Uri(elevationReq.ToString());

                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response2 = null;
            
                response2 = await client.GetAsync(elevationReq);
                string jsonstring2 = await response2.Content.ReadAsStringAsync();

                // Parse the JSON response to extract the address
                dynamic datael = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstring2);
                elevation = datael.elevation[0];
            }
            catch (Exception e)
            {
                loc = "";
                timez = "";
                errmsg = "Invalid Location";
            }
        }
    }
}
.net-core blazor maui
1个回答
0
投票

您缺少尝试使用的 Uri 的格式值。

尝试使用以下代码添加您自己的浮点值,这是该 API 所需的。

// your values here
float latitude = 0; 
float longitude = 0;
Uri elevationReq = new Uri($"https://api.open-meteo.com/v1/elevation?latitude={latitude}&longitude={longitude}");
© www.soinside.com 2019 - 2024. All rights reserved.