分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

netframework转core时文件响应流问题

发布时间:2023-09-06 01:57责任编辑:沈小雨关键词:暂无标签

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved