做将framework webapi项目转成netcore平台上的webapi项目时,发现原来的返回文件响应流在netcore平台下失效。代码如下,返回pdf文件响应流,供前端显示
???/// <summary> ???????/// 根据pdf的预览id获取预览的pdf ???????/// </summary> ???????/// <param name="Id"></param> ???????/// <returns></returns> ???????[HttpGet] ???????[Route("GetPreviewPdf")] ???????public HttpResponseMessage GetPreviewPdf(Guid Id) ???????{ ???????????string pdfBase64String = _cacheManager.Get<string>(Id.ToString()); ???????????var response = new HttpResponseMessage(); ???????????if (!string.IsNullOrEmpty(pdfBase64String)) ???????????{ ???????????????byte[] pdfArray = Convert.FromBase64String(pdfBase64String); ???????????????response.StatusCode = System.Net.HttpStatusCode.OK; ???????????????response.Content = new ByteArrayContent(pdfArray); ???????????????response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(MimeTypes.ApplicationPdf); ???????????} ???????????else ???????????{ ???????????????response.StatusCode = System.Net.HttpStatusCode.Gone; ???????????} ???????????return response; ???????}
不记得参考网址了,反正是stackoverflow上面了。需要在netcore项目中配置支持原有MVC的功能。
1、添加Nuget引用
2、在startup类中添加配置
?????public IServiceProvider ConfigureServices(IServiceCollection services) ???????{ ???????????services.AddMvc().AddWebApiConventions();}
netframework转core时文件响应流问题
原文地址:https://www.cnblogs.com/taoshengyujiu/p/9117808.html