做将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引用

netframework转core时文件响应流问题

 

2、在startup类中添加配置

      public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().AddWebApiConventions();
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-08
  • 2021-08-24
  • 2021-12-27
  • 2022-12-23
  • 2023-03-15
猜你喜欢
  • 2021-10-18
  • 2022-12-23
  • 2021-11-21
  • 2021-05-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案