【发布时间】:2015-06-12 15:40:08
【问题描述】:
我有一个 Web Api 2 端点,它返回包含图像 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"
而不是
data-attr="["http://some.url.com/somepath/ReportStore/image-0.png","http://some.url.com/somepath/ReportStore/image-1.png"]"
【问题讨论】:
-
只使用javascript join() 函数怎么样?
response.Images.join(", "). -
嗯,这是一个有趣的想法,我会试一试。
-
@Jasen 不幸的是,这不起作用,我需要将数组作为字符串本身返回。
标签: c# .net asp.net-web-api asp.net-web-api2