[英]How to override default Content-type
I have a minimal-API ( Project Sdk="Microsoft.NET.Sdk.Web" ) app that has a GET endpoint that I would like to return an image/png.我有一个最小 API(Project Sdk="Microsoft.NET.Sdk.Web")应用程序,它有一个我想返回图像/png 的 GET 端点。
The endpoint is:终点是:
app.MapGet("/text2img/{inputText}", (string inputText) => {
var fileBytes = new ByteArrayContent(text2img(inputText).ToByteArray());
var mimeType="image/png";
var fileBytesArray = fileBytes.ReadAsByteArrayAsync().Result;
return Results.File(new Microsoft.AspNetCore.Mvc.FileContentResult(fileBytesArray, mimeType).FileContents);
});
And it returns the correct bytes for a png-image, but the content-type is octet-stream:它返回 png 图像的正确字节,但内容类型是八位字节流:
HTTP/1.1 200 OK
Content-Length: 24975
Content-Type: application/octet-stream
Date: Fri, 22 Oct 2021 16:29:49 GMT
Server: Kestrel
How do I return the correct Content-type?如何返回正确的内容类型?
Results.File
(at least in the latest version) has parameter for contentType
you can just pass byte array and content type to it: Results.File
(至少在最新版本中)具有contentType
参数,您可以将字节数组和内容类型传递给它:
app.MapGet("/img", () =>
{
var mimeType = "image/png";
var bytes = ...
return Results.File(bytes, contentType: mimeType);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.