IEmunerable的集合<string>导致无效的Web请求



我目前正在通过c#/Visual Studio 2019评估地理编码API(以及PTV Developer API集合中的进一步API)。客户端类已经通过OPEN API生成,我可以成功地收集一个SearchPlacesByTextAsync请求和强制参数。但是,当我尝试使用一些可选参数,如categoryFilter (eenumerable)时,我遇到了错误。我使用List对象来构建参数。知道为什么会崩溃吗?我如何添加一个或多个类别到这个?该错误似乎发生在OPEN API工具链中生成的源代码中。

private void button1_Click(object sender, EventArgs e)
{
try
{
button1.BackColor = System.Drawing.Color.Yellow;
button1.Update();
//
List<string> country = new List<string>();
if (tbxCountry.Text != "")
country.AddRange(tbxCountry.Text.Split(','));
List<string> category = new List<string>();
if (tbxCategoryFilter.Text != "")
category.AddRange(tbxCategoryFilter.Text.Split(','));
string language = (tbxLanguage.Text != "") ? tbxLanguage.Text : null;
PlacesSearchResult result = svcGeocoding.SearchPlacesByTextAsync(tbxText.Text, category, country, language).Result;
dataGridView1.DataSource = result.Places
.OrderBy(o => o.Name)
.Select((p, i) => new
{
i,
p.Name,
p.Address.CountryName,
p.Address.State,
p.Address.Province,
p.Address.PostalCode,
p.Address.City,
p.Address.District,
p.Address.Subdistrict,
p.Address.Street,
p.Address.HouseNumber,
p.FormattedAddress,
CategoryIds = (p.CategoryIds != null) ? String.Join(",",p.CategoryIds) : null,
p.Quality.TotalScore,
ref_Longitude = p.ReferencePosition.Longitude.ToString(coord_tostring),
ref_Latitude = p.ReferencePosition.Latitude.ToString(coord_tostring),
road_Longitude = p.RoadAccessPosition.Longitude.ToString(coord_tostring),
road_Latitude = p.RoadAccessPosition.Latitude.ToString(coord_tostring)
}).ToArray();
//
button1.BackColor = System.Drawing.Color.LimeGreen;
button1.Update();
}
catch (Exception ex)
{
button1.BackColor = System.Drawing.Color.OrangeRed;
button1.Update();
MessageBox.Show(ex.Message, ex.GetType().ToString());
}
}

这是因为您使用了错误的生成器。阅读这里的说明如何创建-net-client-classes-for-access- ptv-developer?

那么代码将按预期工作:

var placesApi = new Geocoding.Api.PlacesApi(new Geocoding.Client.Configuration {
ApiKey = new Dictionary<string, string> {
["apiKey"] = "<your api key>"
}
});
var places = placesApi.SearchPlacesByText("Taxi Karlsruhe", 
categoryFilter: new List<string>{ "navteq-lcms:400-4100-0041", "navteq-lcms:700-7200-0256" },
countryFilter: new List<string>{ "NL", "DE", "BE" });

相关内容

最新更新