Creating a layered design in an application is a fundamental element of modern software architecture. The goal is to promote the Separation of Concerns (SoC) design principle. Separation of Concerns The ideas behind SoC date back to Dijkstra's 1974 paper "On the role of scientific thought" . In computer science, separation of concerns (sometimes abbreviated as SoC) is a design principle for separating a computer program into distinct sections. Each section addresses a separate concern, a set of information that affects the code of a computer program. A concern can be as general as "the details of the hardware for an application", or as specific as "the name of which class to instantiate". A program that embodies SoC well is called a modular program. Modularity, and hence separation of concerns, is achieved by encapsulating information inside a section of code that has a well-defined interface. - Wikipedia SoC is a broad design principal th...
Hosting Javascript Libraries in ASP.NET Applications
Virtually every ASP.NET UI project utilizes Javascript to some degree. Many applications reference JQuery and various Javascript libraries. For most implementations, it's fine to use CDNs as the source for the application's Javascript libraries. Occasionally there are requirements that every element of an application be self-hosted; in other words, the application must be completely self contained. In these scenarios, it's important to automate as much of the Javascript library management as possible.
Node.js, and the Node Package Manager (NPM) specifically, make this relatively straightforward. Most common Javascript libraries are hosted at npmjs.com. When coupled with gulp.js and Visual Studio's Task Runner, you can automate Javascript library management.
Start by making sure Visual Studio 2022 and Node.js are installed on your machine. Next, you'll want to install gulp.js globally using NPM from the command line.
npm install -g gulp-cli
npm install -g gulp
Create or open an ASP.NET project in Visual Studio (creating an ASP.NET project is outside the scope of this post). Open a command prompt in the project's directory and run the following commands to install the Javascript libraries we're going to need.
npm install jquery
npm install bootstrap
Note: As of NPM version 5.0.0, you no long have to explicitly include the --save option when using npm-install. Before NPM 5.0.0, you would have to run npm install --save. So if you're familiar with NPM, --save was intentionally left off the commands above.
Note: For an actual production application, you should specify the versions of the NPM packages and upgrade consistently with intentional care.
At this point, we have the latest version of these Javascript libraries downloaded to our local application. They should have been saved under the node_modules directory inside our project. Additionally, NPM should have created a package.json file inside the project directory that contains information about the Javascript libraries that were installed.
Configure gulp.js
Now lets add gulp.js to the project by running the following from a command line inside the project:
npm install gulp
Let's also add del, which will allow gulp.js tasks to delete files using Javascript.
npm install del
Note: Depending on how the application will be built and deployed, you may want to install gulp and del with the --save-dev option, so these libraries are only installed for the development environment.
Open the package.json file in Visual Studio and manually add a property of "type": "module" to it, as in, the gulpfile.js will be an ES module. This is required to run the Javascript (ECMAScript) in the gulpfile.js that we'll be creating in the next step. The package.json file should now look something like:
If you don't set the "type" to "module", you'll encounter errors like:
SyntaxError: Cannot use import statement outside a module
Now we are ready to configure gulp.js to copy the Javascript libraries to the project's wwwroot directory when we build our application; the Javascript libraries will then be available to our application when running in a web server.
Manually add a gulpfile.js file to your ASP.NET project. This file defines tasks for the gulp.js process. This filename must be gulpfile.js. Below is a sample.
This gulpfile.js defines three basic tasks: clean, copyNodeModules, and defaultTask. The clean task will delete all Javascript libraries from wwwroot\lib. The copyNodeModules task will copy all of the Javascript libraries to wwwroot\lib. Finally, the defaultTask will clean wwwroot\lib, then copy the libraries to wwwroot\lib.
Task Runner Explorer in Visual Studio
Open the Task Runner Explorer in Visual Studio (Ctrl+Alt+Bkspce) and select the UI project that contains the gulpfile.js. You should see the tasks that were exported from the gulpfile.js module (clean, copyNodeModules, and default).
By right-clicking a task, you can run it manually or configure these tasks to be triggered by Visual Studio build actions via Bindings.
After running the default or copyNodeModules task, you should see the Javascript libraries in the wwwroot/lib folder inside of Visual Studio.
Inside your ASP.NET UI Application, you can now reference the Javascript libraries from your local project using HTML similar to: