OdeToCode IC Logo

Serving Up node_modules In ASP.NET

Tuesday, February 2, 2016

The wwwroot folder in ASP.NET isn't the only place for static files. Briefly, in haiku form:

Serve files from anywhere

Configure static file middleware

Each provider thaws the frozen bits of winter

Recently, I wanted to serve files from both wwwroot and node_modules, so I used code like the following in the Startup class:

// this will serve up wwwroot

// this will serve up node_modules
var provider = new PhysicalFileProvider(
    Path.Combine(environemnt.ApplicationBasePath, "node_modules")
var options = new FileServerOptions();
options.RequestPath = "/node_modules";            
options.StaticFileOptions.FileProvider = provider;
options.EnableDirectoryBrowsing = true; 


Note that the UseFileServer method installs static files, default files, and directory browser middleware with a single method call.

You could also use the same technique to serve files from bower_components. Personally, I've stopped using bower and install all client assets using npm for various reasons (including the simplicity, and also Typescript's ability to find modules in node_modules).

Gravatar Mithun Pattankar Tuesday, February 2, 2016
This is really nice idea to be used for serving static files. But how to deal with packaging (deployment) we need to include node_modules also?
Comments are closed.