[英]Can't serve some file extensions from .NET Core local server even after modifying <requestFiltering> in applicationHost.config
I'm building a website and I've come across the problem of not being able to access/download some file extensions like .py
, .cs
.我正在构建一个网站,但遇到了无法访问/下载某些文件扩展名(如.py
、 .cs
。 When I make a request to fetch a .cs file for example..例如,当我请求获取 .cs 文件时..
fetch("https://localhost:44310/userdata/texts/7/637381399366309280.cs")
.then(resp => resp.blob())
.then(blob => {
success(blob)
})
.catch((res) => alert('oh no!'));
userdata
folder is a direct child of wwwroot
folder. userdata
文件夹是wwwroot
文件夹的直接子文件夹。 success
is a function that converts blob to text. success
是一个将 blob 转换为文本的函数。 For a .txt
file (which is in the same folder as the .cs file), everything works fine and I get the exact text the .txt file contains.对于.txt
文件(与 .cs 文件在同一文件夹中),一切正常,我得到了 .txt 文件包含的确切文本。 But for a .cs
file, I get markup text - the index.html of my site.但是对于.cs
文件,我得到了标记文本 - 我网站的 index.html。 Strange thing is oh no!
奇怪的是oh no!
is not alerted.没有被提醒。 I tried with some random .cs file hosted on github and it works perfectly.我尝试了一些托管在 github 上的随机 .cs 文件,它运行良好。
Initially, I got an outright 404. When I tried opening the link ( https://localhost:44310/userdata/texts/7/637381399366309280.cs
) in the browser, I get the page below:最初,我得到了一个 404。当我尝试在浏览器中打开链接( https://localhost:44310/userdata/texts/7/637381399366309280.cs
)时,我得到以下页面:
I did some research and located the applicationHost.config and removed all the filtered extensions such that it looked like this:我做了一些研究并找到了 applicationHost.config 并删除了所有过滤的扩展名,使其看起来像这样:
<requestFiltering>
<fileExtensions allowUnlisted="true" applyToWebDAV="false">
<!--empty-->
</fileExtensions>
....
</requestFiltering>
Now I don't get a 404 anymore.现在我不再收到 404 了。 But success(blob)
returns markup text - the index.html of my site.但success(blob)
返回标记文本 - 我网站的 index.html。 (I use Reactjs and I've noticed this happens when something goes wrong with the fetch.) When I try opening the url in a new tab, I don't get the requestFiltering
error page anymore. (我使用 Reactjs,我注意到当获取出现问题时会发生这种情况。)当我尝试在新选项卡中打开 url 时,我不再收到requestFiltering
错误页面。 Instead, the home page of the site loads.而是加载站点的主页。
So I'm really confused.所以我真的很困惑。 I don't get a 404, but I don't seem to be getting the file either.我没有收到 404,但我似乎也没有收到文件。 Modifying the requestFiltering
changed something, but it seems there's something else I need to do.修改requestFiltering
改变了一些东西,但似乎还有一些我需要做的事情。 Please help.请帮忙。
Turns out that in addition to editing the <requestFiltering>
in the applicationHost.config file, I also have to ServeUnknownFileTypes
in the startUp class.原来,除了编辑<requestFiltering>
applicationHost.config文件中的,我也有ServeUnknownFileTypes
在启动类。
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "text/plain"
});
Check out this answer to learn more.查看此答案以了解更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.