Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with serving some static files within ASP.NET Core MVC

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" });
            });
        }
like image 938
Sergey Barskiy Avatar asked Aug 31 '16 02:08

Sergey Barskiy


1 Answers

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.

like image 184
Sergey Barskiy Avatar answered Oct 13 '22 10:10

Sergey Barskiy