Cannot import libraries from Node_Modules to ASP.NET CORE APP

I’m learning Scott Allen – ASP.Net Core Fundamental course from Pluralsight and trying to add the external library from Node_Modules to my project.

Here is the project’s structure:

Cannot import libraries from Node_Modules to ASP.NET CORE APP

And how I including datatable library to my project (adding to Pages/Shared/_Layout.cshtml)
Cannot import libraries from Node_Modules to ASP.NET CORE APP

But when the app is running, it cannot find these libraries:
Cannot import libraries from Node_Modules to ASP.NET CORE APP

Questions:

  1. What wrong with this way ?
  2. Can you guys suggest some effective ways to import external libraries to ASP.NET CORE app?

I’m using .NET 5

<PropertyGroup>
   <TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Please note that the default directory that we store static files is {content root}/wwwroot, you can move these datatables plugin related js&css files to web root directory.

For more information about serving static files in ASP.NET Core, please check this doc:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-5.0

Method 2

From the document provided by Fei Han I solved my problem. I need add some config to include “node_modules” as PhysicalFileProvider in Startup.cs file like below:
Cannot import libraries from Node_Modules to ASP.NET CORE APP

and then update src and href attribute in script/link tag:

<script src="~/StaticFiles/datatables.net/js/jquery.dataTables.js"></script>
<link href="~/StaticFiles/datatables.net-bs4/css/dataTables.bootstrap4.css" rel="stylesheet"/>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x