我试图在webapi上实现文件返回GET。通过最终实现,只需将图像标签中的src
添加到api get端点即可。即
<img src='myapi/api/file/{id}' />
我遵循了以下堆栈问题;
Return an image using ASP.NET Web API and display it?
我的控制器代码如下;
[HttpGet]
[ProducesResponseType(typeof(HttpResponseMessage), 200)]
[Route("{id}")]
public HttpResponseMessage Get(int id)
{
var response = _service.Get(id);
HttpAssert.Success(response);
HttpAssert.IsNotNull(response);
var result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(response.ByteArray);
result.Content.Headers.ContentType = new MediaTypeHeaderValue(response.MimeType);
return result;
// return Ok(response.Result);
}
我的服务以具有正确的mime类型关联的字节数组形式返回文件,但是当我在邮递员或网络浏览器中调用此文件时,会得到以下返回值;
{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Type",
"value": [
"image/jpeg"
]
}
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [],
"requestMessage": null,
"isSuccessStatusCode": true
}
有人可以指导我关于我在这里做错了什么吗?