我有 2 个 Web API,它们具有完全相同的代码和相同的引用。
- 第一个(称为项目 A)在 .net 4.6 下运行,是旧的 Web API 项目
- 第二个项目(称为项目 B)也在 .net 4.6 下运行,但它是新的 .Net core Web API 项目。
我使用了与下面相同的引用
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
这是他们都使用的代码。
public HttpResponseMessage TestReport()
{
HttpResponseMessage response;
PdfDocument document = HelloWorld();
using (MemoryStream memoryStream = new MemoryStream()) {
document.Save(memoryStream, false);
byte[] buffer = memoryStream.GetBuffer();
var contentLength = buffer.Length;
response = new HttpResponseMessage(HttpStatusCode.OK);
// Byte Array Test
//response.Content = new ByteArrayContent(buffer);
//response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
//response.Content.Headers.ContentLength = contentLength;
//response.Content.Headers.ContentDisposition.FileName = "anything.pdf";
//response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
//var test = response.Content.ReadAsStreamAsync();
// Stream Content Test
response.Content = new StreamContent(new MemoryStream(buffer));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
response.Content.Headers.ContentLength = contentLength;
ContentDispositionHeaderValue contentDisposition = null;
if (ContentDispositionHeaderValue.TryParse("attachment; filename=" + "helloWorld" + ".pdf", out contentDisposition)) {
response.Content.Headers.ContentDisposition = contentDisposition;
}
return response;
};
}
public PdfDocument HelloWorld()
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
//XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
// Create a font
XFont font = new XFont("Times New Roman", 20, XFontStyle.BoldItalic);
// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.Center);
// Save the document...
const string filename = "HelloWorld_tempfile.pdf";
//document.Save(filename);
return document;
}
当我调用它们时(无论是从另一个 Controller 还是从浏览器),它们会产生 2 个不同的结果
- 项目A返回Pdf文件,正确的响应头是application/pdf
- 无论我在响应头中设置什么,项目 B 总是返回 Json (application/json) 格式。我尝试过 xml、文本、流、字节数组。什么都不起作用。
我的问题是:
- 这到底是怎么回事?又是微软@#%^&吗?
- 我能为项目 B 做些什么来返回正确的响应(按照我的定义)
P/S:这是我的 web.config 文件
项目 B:模板中的默认值(我无法将其粘贴到此处,不知道为什么)
项目 A:模板中的默认值(我无法将其粘贴到此处,不知道为什么)
最佳答案
asp.net-core默认不支持HttpResponseMessage
,这是来自以前版本的Web API。 Core 将操作结果中的 HttpResponseMessage
视为模型,并将其序列化为 JSON。
重构项目B代码...
public IActionResult TestReport() {
var document = HelloWorld();
using (var memoryStream = new MemoryStream()) {
document.Save(memoryStream, false);
var buffer = memoryStream.GetBuffer();
var contentDisposition = new ContentDispositionHeaderValue("attachment");
contentDisposition.SetHttpFileName("helloWorld.pdf");
Response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString();
return File(buffer, "application/pdf");
}
}
关于c# - 为什么Web Api(.Net Core)总是返回JSON而忽略ContentType中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44225731/