首页 > 解决方案 > .Net核心请求过滤和文件下载

问题描述

我们有一个 .net 核心 Web 应用程序,它只是为我们的一些客户端应用程序更新托管文件。

我们决定在其中一个客户端应用程序中添加应用程序洞察,文件 ApplicationInsights.config 是更新的一部分。

对https://server/path/to/update/ApplicationInsights.config的请求会引发 404 错误。

到目前为止,我已经尝试过:

  1. 在启动时在静态文件定义中添加“.config”扩展名:无效(这适用于 .exe 和 .dll)
  2. 启用此文件夹的文件夹浏览,仍然没有效果

它似乎与一些开箱即用的请求过滤有关。

问题是 :

如何禁用特定文件夹的所有下载限制(最佳)

或者

如何禁用 *.config 文件的所有过滤

先感谢您

标签: asp.net-coreweb-applications

解决方案


这是因为默认FileExtensionContentTypeProvider不提供*.config文件映射。

要使其服务*.config文件,只需创建您自己的ContentTypeProvider,或添加映射*.config

var myContentTypeProvider= new FileExtensionContentTypeProvider();
myContentTypeProvider.Mappings.Add(".config","text/plain");

app.UseStaticFiles(new StaticFileOptions{
    RequestPath = "/path/to/update",
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(),"path/to/update"),
        ExclusionFilters.None
    ),
    ContentTypeProvider = myContentTypeProvider,
});

[更新]

经过讨论,以下Web.Config(由OP)有效:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.webServer>
        <security>
            <requestFiltering>
                <fileExtensions>
                    <remove fileExtension=".config" /> 
                    <add fileExtension=".config" allowed="true" />
                </fileExtensions>
            </requestFiltering> 
        </security> 
    </system.webServer> 
</configuration>

推荐阅读