我将尝试将查询字符串传递到BaseAddress中,但它无法识别引号“?”。
这个引号会破坏URI。
首先,我创建了我的BaseAddress。
httpClient.BaseAddress = new Uri($"https://api.openweathermap.org/data/2.5/weather?appid={Key}/");
然后我调用GetAsync方法,尝试添加另一个参数。
using (var response = await ApiHelper.httpClient.GetAsync("&q=mexico"))....
这是代码调用的URI
https://api.openweathermap.org/data/2.5/&q=mexico
BaseAddress
属性,你需要使用以System.Uri
作为参数的GetAsync
方法。它将解析您的部分URL并将其附加到基础URL上。您的查询字符串应该以问号?q=mexico&other=2
开头。 - Glenn FerrieDelegatingHandler
,因为我认为这个功能不可能在没有处理程序或在每个请求中手动附加到URL的情况下实现。 - ProgrammingLlama