Installing Client-Side Libraries in ASP.NET Core
Microsoft products have been famously difficult to work with at times. One dev offers a solution to the round-about way of using client-side libraries in ASP.NET Core.
Join the DZone community and get the full member experience.Join For Free
As a relative newcomer to the web front-end, one thing that always surprised me was how many moving parts you need to get something running. This is probably true elsewhere (i.e. in backend development and desktop development) but we just do a better job of hiding it. In the past, people writing web-pages have always had an uneasy relationship with Microsoft. Maybe it started in the late '90s when, in order to win the battle of the browsers with Netscape, Microsoft started giving its product away. Since then, IE has become more and more bloated, as it had to support 10 years worth of old technology, and people have had to ensure it still worked on IE 6 and 7.
But now things are different, Microsoft is a rebranded company and nobody hates them anymore... and when you create a brand new ASP.NET Core 2.1 project, you can use npm to install client-side packages, use them in your web page, and it all just works — end of the post.
Except that hasn't happened. In fact, the client-side package management for the web seems (to me) to be in a bit of a mess — especially where Microsoft is concerned.
Create a brand new ASP.NET Core 2.1 MVC project from the template and it comes with jQuery, supplied by Bower; except Microsoft isn't continuing support for Bower anymore.
So, use NuGet to install your package?
Nope, not on .NET Core!
So, use npm, that's still supported, right?
Yep. Here's the ASP.NET Signal R client-side package installed using npm:
Okay, so it puts it in 'node_modules' — I can reference the library directly from there, right?
Nope, it needs to be in
Errr, so I copy it across manually?
You can. Although that's kind of the problem that package managers were invented to solve.
I can create a Gulp task to take the files out of the downloaded directory and place them into the lib directory!
Yes, yes you can. Although now you have a Gulp task, and an npm restore, all so that you can include one or two files in your project. This all just seems too hard!
Introducing LibMan. It isn't a Package Manager; but it might just be the correct answer to the question: "Why the hell is this whole thing so difficult. I only want one file!"
Here is the context menu for the lib folder after v15.8 (this has been available in preview for a while):
That gives you the following dialog:
This is amazing, I can pick the library that I want, and where I want it to go! I can also select specific files that I want — this almost feels like a sensible way to manage client-side packages. Click install and bang:
You can see what it's done by either selecting "Manage Client Side Packages..." from the solution context menu:
Alternatively, you can just have a look at the libman.json file (they both do the same thing as of 15.8.7, which makes me wonder whether "Manage Client Side Packages..." will do something different one day — the ellipses kind of give it away).
There's a CLI for it (which seemed to balk a bit when I tried to install it via NuGet), and you can use this as a replacement for Gulp to copy files around, by just selecting "File System" as the source (although this sort of solves that problem further down the tree).https://docs.microsoft.com/en-us/aspnet/core/client-side/bower?view=aspnetcore-2.1
Published at DZone with permission of Paul Michaels, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.