C# 将字符串列表从客户端传递到web api

C# 将字符串列表从客户端传递到web api,c#,asp.net-web-api,C#,Asp.net Web Api,客户端应用程序访问web api控制器以获取一组数据,该控制器有一个列表作为参数 static void Main(string[] args) { Program p = new Program(); p.getdata().Wait(); } public async Task getdata() { List<string> datelist = new List<string>(); datelist.Add("12/05/2

客户端应用程序访问web api控制器以获取一组数据,该控制器有一个列表作为参数

static void Main(string[] args)
{

    Program p = new Program();
    p.getdata().Wait();
}


public async Task getdata()
{
    List<string> datelist = new List<string>();
    datelist.Add("12/05/2017");
    datelist.Add("14/05/2017");
    datelist.Add("18/05/2017");

    HttpClient host = new HttpClient();
    host.BaseAddress = new Uri("http://localhost/widgetApi/");
    host.DefaultRequestHeaders.Clear();
    host.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    StringContent content = new StringContent(JsonConvert.SerializeObject(datelist), Encoding.UTF8, "application/json");



    HttpResponseMessage response = await host.GetAsync("api/dataAccessApi?"+ datelist);
    response.EnsureSuccessStatusCode();
   if( response.IsSuccessStatusCode)
    {

        Console.Read();
    }
}
static void Main(字符串[]args)
{
程序p=新程序();
p、 getdata().Wait();
}
公共异步任务getdata()
{
列表日期列表=新列表();
日期列表。添加(“2017年5月12日”);
日期列表。添加(“14/05/2017”);
日期列表。添加(“2017年5月18日”);
HttpClient主机=新HttpClient();
host.BaseAddress=新Uri(“http://localhost/widgetApi/");
host.DefaultRequestHeaders.Clear();
host.DefaultRequestHeaders.Accept.Add(新的System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(“应用程序/json”);
StringContent=newStringContent(JsonConvert.SerializeObject(datelist),Encoding.UTF8,“application/json”);
HttpResponseMessage response=wait host.GetAsync(“api/dataAccessApi?”+日期列表);
response.EnsureSuccessStatusCode();
if(响应。IsSuccessStatusCode)
{
Console.Read();
}
}
控制器是

public HttpResponseMessage Get([FromBody] List<string> dates)
{
   ..... function going here
}
public HttpResponseMessage Get([FromBody]列表日期)
{
…函数在这里运行
}

我的问题是如何将日期列表传递给web api???

GET请求没有正文,可以使用查询字符串值<代码>?日期=“{urlcoded date}”和日期=“{urlcoded date}”…

public HttpResponseMessage Get([FromUri] List<string> dates) {
   //..... function going here
}
使用
UrlEncode
如下所示

/// <summary>Escape RFC3986 String</summary>
static string UrlEncode(string stringToEscape) {
    return stringToEscape != null ? Uri.EscapeDataString(stringToEscape)
        .Replace("!", "%21")
        .Replace("'", "%27")
        .Replace("(", "%28")
        .Replace(")", "%29")
        .Replace("*", "%2A") : string.Empty;
}
///转义RFC3986字符串
静态字符串UrlEncode(字符串stringToEscape){
返回stringToEscape!=null?Uri.EscapeDataString(stringToEscape)
.替换(“!”,“%21”)
.替换(“'”,“%27”)
.替换(“(”,“%28”)
.替换(“)”,“%29”)
.Replace(“*”,“%2A”):string.Empty;
}

对于[FromQuery]而不是[FromUri]

GET请求没有正文,可以使用查询字符串值<代码>?日期=“{urlcoded date}”和日期=“{urlcoded date}”…Get([FromUri]列表日期)控制器参数日期为空数组,
/// <summary>Escape RFC3986 String</summary>
static string UrlEncode(string stringToEscape) {
    return stringToEscape != null ? Uri.EscapeDataString(stringToEscape)
        .Replace("!", "%21")
        .Replace("'", "%27")
        .Replace("(", "%28")
        .Replace(")", "%29")
        .Replace("*", "%2A") : string.Empty;
}