I am building a webpage using ASP.NET Core v2 and would like to use font-awesome.
First let me say that I have tried several things. like installing Bower from NPM, installing font-awesome from NPM, installing font-awesome from Bower packages in VS but nothing seem to work.
can someone please provide the correct way to install font-awesome, (preferred without using a lot of console commands or manual copying of files.)
This is what my depedencises currently looks like
Go to package.json
add the following tag "font-awesome": "^4.7.0" in dependencies section
Eg :-
As a recommendation from Microsoft
Open the
bower.json
file and addfont-awesome
to the dependenciesOpen the
.bowerrc
file underbower.json
, Thedirectory
property is set towwwroot/lib
which indicates the location Bower will install the package assets.Now you can refer font-awesome as below
The easiest way to get started with Font Awesome is to add a reference to it, using its public content delivery network (CDN) location:
For more details:
https://docs.microsoft.com/en-us/aspnet/core/client-side/bower
I am using ASP.NET Core 2.0.8, on Windows 10, and just struggled to get FA in to my project too. I was able to add it to NPM by adding it to the package.json (as others have mentioned) under dependencies, like this:
Unfortunately, although it restored the package, it didn't deploy the css files where I had expected, so that I could reference them. I don't believe the following is anything like the "correct" way to do it (still learning!), however, it worked for me.
In Solution Explorer:
You can now drag these in to your project to make references to them, and start using Font Awesome, like this:
_Layout.cshtml:
Hope this is of some help to anyone stopping by like I was.
This is for .NET Core 2, after you create a SPA Project using dotnet new angular:
Go to the root of the project and install the package: npm install font-awesome --save. You should now see it in package.json dependencies.
After that go to webpack.config.vendor.js and add font awesome to the array under non tree shakable modules:
const nonTreeShakableModules = [ 'bootstrap', 'bootstrap/dist/css/bootstrap.css', 'es6-promise', 'es6-shim', 'event-source-polyfill', 'font-awesome/css/font-awesome.css', 'jquery', ];
Now we need to tell the webpack that we added this new package. So if you haven't done so before install this in the root of project with npm install --save-dev npm-install-webpack-plugin.
Finally, run this command in the root of project: webpack --config webpack.config.vendor.js
Bower
is going away.NuGet
now focus on .NET packages so your best bet to getfont-awesome
is viaNPM
.1.Download NPM
Make sure you have the latest
nodejs
installed. Navigate to https://nodejs.org/en/ and download the LTS version.To check the version of
node
as well asnpm
installed on your machine:node -v
andnpm -v
2. Add
package.json
You need to have the npm configuration file to your project. This configuration file lists out all your packages and is used by npm to restore packages when it needs to.
To add the npm configuration file:
3. Install font-awesome (and others)
Open
package.json
and underdevDependencies
, just by typing"font-awesome":
, it should automatically give you a list of available versions you can pick. Pick a version you like.By saving this
package.json
file, thenpm
will download the libraries listed into the hiddennode_modules
folder under your project.4. Copy files to wwwroot
In ASP.NET Core MVC applications, if you want to use static contents like styles and javascript files, beside you need to enable
app.UseStaticFiles();
in theStartup.cs
, you also need to copy the files to the wwwroot folder. By default, the content, for example, innode_modules
are not serviceable to your application.You can of course manually cope the files you want into wwwroot folder. But then you will have to replace the files whenever there are new versions come out.
To properly copy the files, we need to use 3rd party tooling!
BundlerMinifier is a great tool you can use to bundle files you want and output them to the directories you want. It uses similar configuration file called
bundleconfig.json
:But this tool doesn't work well with file types other than
.css
and.js
. There is also an issue of relative url to the font-awesome fonts folder because the font-awesome style usesurl();
(setting"adjustRelativePaths": false
doesn't always work).5. Create Gulp tasks
To properly move font-awesome files and fonts to wwwroot folder, I can use
gulp
to set up tasks I can run before build, after build, etc:5.1. Install
gulp
(andrimraf
, the unix commandrm -rf
)Go to
package.json
and typegup
andrimraf
in the dependency list. Save the file.5.2. Add
gulpfile.js
to your project and define tasksI basically need to create a task to move font-awesome to wwwroot/libs folder, and create another task to do the reverse for cleanup. I omitted other tasks not related to font-awesome.
5.3 Run tasks
After you have setup the
gulpfile.js
, you should be able to see the tasks listed in "Task Runner Explorer". If you don't have that window in Visual Studio, Go to "View" -> "Other Windows".You can manually run any task by right clicking it and hit "Run". Or you can have it run automatically when before build, after build, clean or project open.
After you setup the bindings, a header would be added to the
gulpfile.js
:Now save the
gulpfile.js
file and build your project. Your font-awesome files and fonts should be automatically moved to the wwwroot and ready to use.I would recommend using LibMan for this (Short documentation how to use it).
I wrote it manually, bet you could add it through "Add -> Client-Side Library". Here is mine libman.json