简体   繁体   English

ASP.NET Core-防止提供某些文件类型的静态文件

[英]ASP.NET Core - prevent static file of certain filetype from being served

I'm attempting to configure ASP.NET Core to disallow serving .map files in certain situations. 我正在尝试将ASP.NET Core配置为在某些情况下不允许提供.map文件。 I have attempted to remove it from the known filetypes, but that doesn't seem to prevent it from actually serving the file. 我试图将其从已知的文件类型中删除,但这似乎并不能阻止它实际为文件提供服务。 This is what I have now: 这就是我现在所拥有的:

        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "Client";

            if (env.IsDevelopment())
            {
                var customPort = 60001;
                spa.UseProxyToSpaDevelopmentServer($"http://localhost:{customPort}");
                spa.UseReactDevelopmentServer("start");
            }
        });

        var contentTypeProvider = new FileExtensionContentTypeProvider();
        contentTypeProvider.Mappings.Remove(".map");
        app.UseSpaStaticFiles(new StaticFileOptions
        {
            ContentTypeProvider = contentTypeProvider
        });

With this configuration, I am still able to load .map files when I browse to them. 使用此配置,当我浏览.map文件时,我仍然能够加载它们。 What can I do to prevent this? 我该怎么做才能避免这种情况?

Try using UseStaticFiles instead, before app.UseSpa call and even before you call app.UseMvc if applicable. 尝试使用UseStaticFiles代替,前app.UseSpa电话,打电话甚至在app.UseMvc (如果适用)。 I presume .map is for css/scss mappings. 我认为.map用于CSS / SCSS映射。 This is not particularly an SPA file, hence it is being served. 这不是特别的SPA文件,因此正在提供。

var contentTypeProvider = new FileExtensionContentTypeProvider();
contentTypeProvider.Mappings.Remove(".map");

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

app.UseMvc(); // if applicable

app.UseSpa(spa =>
{
    spa.Options.SourcePath = "Client";

    if (env.IsDevelopment())
    {
        var customPort = 60001;
        spa.UseProxyToSpaDevelopmentServer($"http://localhost:{customPort}");
        spa.UseReactDevelopmentServer("start");
    }
});

app.UseSpaStaticFiles();

I hope this helps 我希望这有帮助

The issue here was that I was using the local development setup which proxies through Node. 这里的问题是我正在使用通过Node代理的本地开发设置。 When this is the case, the proxy handles the static files instead of ASP.NET Core. 在这种情况下,代理将处理静态文件而不是ASP.NET Core。 I had to switch it up and use the published code to test. 我不得不将其切换并使用发布的代码进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM