c#web api如何将url作为参数传递

时间:2016-01-15 00:42:11

标签: c# asp.net asp.net-web-api

我有一个c#Web api,就像这样:

[HttpGet]
[Route("gatewayinfo/{endpointAddress}")]
public GatewayModel GetGatewayInfo(string endpointAddress)

当我打电话给api时,我需要传递一个网址作为地址(http://example.con

如何使用HttpClient做到这一点?如果我把url放在参数中它就不会工作:

var client = new HttpClient();
client.SetBearerToken(token);
var result = await client.GetStringAsync(_webApiAddress + parameter);

1 个答案:

答案 0 :(得分:3)

您需要对param

进行编码
using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(_webapiAddress);
    client.SetBearerToken(token)

    var result  = await client.GetStringAsync($"gatewayinfo/{HttpUtility.UrlEncode(parameter)}");
}