问题
我想在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
。