ASP.NET 核心静态文件 Return 作为 Base64 编码

ASP.NET Core Static File Return as Base64 Encoded

我创建了一个新的 ASP.NET 核心网络应用 dotnet new webapp
我给它添加了 d3.js 库,没问题。

当我尝试 运行 日历示例时,它会尝试从服务器加载 CSV 文件,我已将该文件添加到 wwwroot 目录中,但是当响应从开发服务器,它是 base64 编码的。

这会导致 d3 的 JSON 解析中断。

This is the GitHub repo of the project if you want to run and see the error.

知道为什么 Kestrel 以这种方式编码文件吗?

不确定 d3.csv() 的作用,但您可能只需要正确设置 Content-Type 即可。默认情况下,Content-Type: application/octet-stream 用于未知的文件类型。

在 Startup.cs 中的 Configure 方法中,为 .csv 文件类型设置映射。

var provider = new FileExtensionContentTypeProvider();

// Add csv mapping
provider.Mappings[".csv"] = "text/csv"; // Try text/plain if text/csv doesn't work

app.UseStaticFiles(new StaticFileOptions
{
    ContentTypeProvider = provider
});