简体   繁体   中英

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. 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. What can I do to prevent this?

Try using UseStaticFiles instead, before app.UseSpa call and even before you call app.UseMvc if applicable. I presume .map is for css/scss mappings. This is not particularly an SPA file, hence it is being served.

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. When this is the case, the proxy handles the static files instead of ASP.NET Core. I had to switch it up and use the published code to test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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