netframework转core时文件响应流问题

本文介绍如何解决从framework webapi项目迁移至NetCore平台时,返回PDF文件响应流功能失效的问题。通过添加Nuget引用及配置支持原有的MVC功能,确保文件流正常返回。

做将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();
}

 

转载于:https://www.cnblogs.com/taoshengyujiu/p/9117808.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值