Asp.net web api webapi中文件下载的内容协商
我有一个WebAPI REST服务方法,它返回pdf文件。代码如下:Asp.net web api webapi中文件下载的内容协商,asp.net-web-api,content-negotiation,Asp.net Web Api,Content Negotiation,我有一个WebAPI REST服务方法,它返回pdf文件。代码如下: string content = some byte array; HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StringContent(content); //a text file is actually
string content = some byte array;
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StringContent(content);
//a text file is actually an octet-stream (pdf, etc)
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
//we used attachment to force download
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "mypdf.pdf";
return result;
我的疑问是,对于API中的其他方法,我使用内容协商作为响应的媒体类型。我也需要在这里使用内容协商吗??这里需要还是不需要?不需要。Conneg完全是可选的。如果您的资源只有一个
应用程序/pdf
表示形式,那就这样吧。我同意下面的Darrel…还要注意,如果您的内容是字节数组,您可以使用ByteArrayContent