问题
我想在我的 ASP.Net Web API 控制器中返回一个文件,但我的所有方法都将HttpResponseMessage
作为 JSON 返回。
代码到目前为止
public async Task<HttpResponseMessage> DownloadAsync(string id)
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent({{__insert_stream_here__}});
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return response;
}
当我在浏览器中调用此端点时,Web API 将HttpResponseMessage
作为 JSON 返回,并将 HTTP Content Header 设置为application/json
。
1 回答
如果这是 ASP.net-Core,那么您正在混合 Web API 版本。让操作返回派生的
IActionResult
,因为在当前代码中,框架将HttpResponseMessage
视为模型。