简体   繁体   English

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

[英]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.

相关问题 ASP.NET 核心 web.config requestFiltering 不覆盖 applicationhost.config - ASP.NET Core web.config requestFiltering not overriding applicationhost.config 重置applicationhost.config后,ASP.NET Core返回404 - ASP.NET Core returns 404 after resetting applicationhost.config 如何将IIS ApplicationHost.config文件解析为.Net对象? - How to parse the IIS ApplicationHost.config file into .Net objects? Applicationhost.config未更新 - Applicationhost.config not updated 由于applicationHost.config无效,.Net构建失败 - .Net build is failing due to invalid applicationHost.config applicationhost.config中的位置路径未映射到程序集 - Location path in applicationhost.config not mapping to assembly C# applicationHost.config 未找到但存在 - C# applicationHost.config not found but exists 以编程方式解锁applicationHost.config中的部分 - Unlock section in applicationHost.config programmatically 初始化 applicationhost.config 文件失败。 找不到 IIS Express - Initializing the applicationhost.config file failed. Cannot find IIS Express applicationHost.config 错误:由于 IIS 共享配置的权限不足,无法写入配置文件 - applicationHost.config Error: Cannot write configuration file due to insufficient permissions with IIS shared configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM