C# 使用Web Api 2以字符串形式返回数组

C# 使用Web Api 2以字符串形式返回数组,c#,.net,asp.net-web-api,asp.net-web-api2,C#,.net,Asp.net Web Api,Asp.net Web Api2,我有一个WebAPI2端点,它返回包含图像URL数组的Json。它的反应是这样的 { "Status": 0, "Message": "Success", "Images": [ "http://some.image.com/somepath/SomeReport-0.png", "http://some.image.com/somepath/SomeReport-1.png" ] } 我希望json数组作为数组字符串

我有一个WebAPI2端点,它返回包含图像URL数组的Json。它的反应是这样的

   {
    "Status": 0,
    "Message": "Success",
    "Images": [
        "http://some.image.com/somepath/SomeReport-0.png",
        "http://some.image.com/somepath/SomeReport-1.png"
    ]
   }
我希望json数组作为数组字符串返回。为了实现这一点,我需要如何设置我的响应?我目前使用此方法设置数组和响应

// response.status and response.message are added before this

response.Images = imagePaths.Select(x => string.Format(urlString, Path.GetFileName(x)));

HttpResponseMessage apiResponse = Request.CreateResponse(HttpStatusCode.OK, response);
需要以字符串格式返回数组的目的是为了将其附加到html数据属性中。现在,如果我这样引用响应:

listItem.append('<a class="dd-option" data-path="' + response.Images + '" href="#"></a>');
而不是

data-attr="["http://some.url.com/somepath/ReportStore/image-0.png","http://some.url.com/somepath/ReportStore/image-1.png"]"

只需将图像映射到字符串数组并从控制器返回该对象

public string[] Get()
{
     //create this array object from your images object
     return new string[]{"imagePath1", "imagePath2", "imagePath3"};
}

如果使用javascript执行此操作,则可以使用JSON.stringify

var jsonResponse=
{
“状态”:0,
“消息”:“成功”,
“图像”:[
"http://some.image.com/somepath/SomeReport-0.png",
"http://some.image.com/somepath/SomeReport-1.png"
]
}
var images=JSON.stringify(jsonResponse.images);
控制台日志(图像);
$(“#foo”).append($(“
  • ”).text(“foo”).attr(“数据foo”,图像))
    
    

    是否可能重复使用javascript join()函数
    response.Images.join(“,”)
    .hmm这是一个有趣的想法,我将试一试。@Jasen很遗憾,这不起作用,我需要数组本身作为字符串返回。在c中也可以
    public string[] Get()
    {
         //create this array object from your images object
         return new string[]{"imagePath1", "imagePath2", "imagePath3"};
    }