重命名NET Core 2.2中请求的参数

时间:2019-03-05 14:41:19

标签: c# .net-core asp.net-core-2.2

我正在构建一个简单的代理,用于将两个获取请求发送到数据提供者OpenWeatherMap。根据其文档,如果要获取当前天气,则需要发送带有参数q的请求。当前,我使用Axios库从前端部件发出请求,并在此指示此q参数。但我想使其更具可读性,并使用参数cityName发送请求。如何在应用程序的NET Core部分中更改参数名称?

这是我在HttpClient中所做的:

using (var httpClient = new HttpClient())
{
    var response = await httpClient.GetAsync( "http://api.openweathermap.org/data/2.5/weather" + pathAndQuery.Replace( apiEndpoint, "" ) + "&appid=ggggg" );
    var result = await response.Content.ReadAsStringAsync();
    context.Response.StatusCode = (int)response.StatusCode;
    await context.Response.WriteAsync( result );
}

1 个答案:

答案 0 :(得分:1)

您可以编写这样的方法:

public const string Endpoint = "api.openweathermap.org/data/2.5/weather";
public async void GetWeatherBytCityName(string cityName)
{
    using (var httpClient = new HttpClient())
    {
        var query = $"?q={cityName}";
        var response = await httpClient.GetAsync( $"{Endpoint}{query}");
    }
}