即使在 applicationHost.config 中修改 <requestFiltering> 后,也无法从 .NET Core 本地服务器提供某些文件扩展名

Can't serve some file extensions from .NET Core local server even after modifying <requestFiltering> in applicationHost.config

我正在建立一个网站,我遇到了无法 access/download 某些文件扩展名的问题,例如 .py.cs。例如,当我请求获取 .cs 文件时..

fetch("https://localhost:44310/userdata/texts/7/637381399366309280.cs")
        .then(resp => resp.blob())
        .then(blob => {
            success(blob)
        })
        .catch((res) => alert('oh no!'));

userdata 文件夹是 wwwroot 文件夹的直接子文件夹。 success 是一个将 blob 转换为文本的函数。对于 .txt 文件(与 .cs 文件位于同一文件夹中),一切正常,我得到了 .txt 文件包含的确切文本。但是对于 .cs 文件,我得到标记文本 - 我网站的 index.html。奇怪的是 oh no! 没有被提醒。我尝试使用 github 上托管的一些随机 .cs 文件,它运行良好。

最初,我得到了一个彻底的 404。当我尝试在浏览器中打开 link (https://localhost:44310/userdata/texts/7/637381399366309280.cs) 时,我得到以下页面:

我做了一些研究并找到了 applicationHost.config 并删除了所有过滤后的扩展名,使其看起来像这样:

<requestFiltering>
   <fileExtensions allowUnlisted="true" applyToWebDAV="false">
          <!--empty-->
   </fileExtensions>
   ....
</requestFiltering>

现在我不再收到 404 了。但是 success(blob) returns 标记文本 - 我网站的 index.html。 (我使用 Reactjs,我注意到当提取出现问题时会发生这种情况。)当我尝试在新选项卡中打开 url 时,我不再看到 requestFiltering 错误页面。而是加载网站的主页。

所以我真的很困惑。我没有收到 404,但我似乎也没有收到该文件。修改 requestFiltering 改变了一些东西,但似乎我还需要做一些其他事情。请帮忙。

原来除了编辑 applicationHost.config 文件中的 <requestFiltering> 外,我还必须 ServeUnknownFileTypes 在 startUp class.

app.UseStaticFiles(new StaticFileOptions
{
   ServeUnknownFileTypes = true,
   DefaultContentType = "text/plain"
});

查看 了解更多信息。