6
votes

I am having a problem with ASP.NET Core not serving static files properly. I have parts of my app in node_modules under wwwroot. For the most part all files work, but there are exceptions. *.js.map files are routed to MVC controller, serving my MVC pages instead of actual files. As a result, I get errors in the browser such as

Failed to parse SourceMap: http://localhost:5000/node_modules/bootstrap/bootstrap.min.css.map

Going the same route, my web fonts, such as the one includes with Bootstrap are also not served properly, being also handled by MVC middleware instead of static files middleware. It seems that all files that reside in node_modules should be routed to my static files middleware, which is not happening. Thanks.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(env.WebRootPath, @"node_modules")),
                RequestPath = new PathString("/node_modules"),
                ServeUnknownFileTypes = true
                
            });

            app.UseMvc(config =>
            {

                config.MapRoute("Default", "{controller}/{action}/{id?}",
                    new { controller = "Home", action = "Index" });

                config.MapRoute("AngularDeepLinkingRoute", "{*url}",
                    new { controller = "Home", action = "Index" });
            });
        }
2

2 Answers

1
votes

The problem was that if a static file, such as *.js.map file is missing, static files middleware does not handle the request, and it goes to MVC middleware.

0
votes

Remove the following the code, it is not required at all

app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(env.WebRootPath, @"node_modules")),
            RequestPath = new PathString("/node_modules"),
            ServeUnknownFileTypes = true

        });

Use the following code only

app.UseStaticFiles();

If node_module directory is under wwwroot directory then content inside node_module will be treated as static content. Static files are stored within your project's web root directory. The default directory is /wwwroot.

For details refer to following link Microsoft Docs - Working with static files