我给你两个答案。npm与其他工具结合在一起功能强大,但是需要一些工作来设置。如果只想下载某些库,则可能要改用“ 库管理器”(在Visual Studio 15.8中发行)。
NPM(高级)
首先在您的项目的根目录中添加package.json。添加以下内容:
{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"devDependencies": {
"gulp": "3.9.1",
"del": "3.0.0"
},
"dependencies": {
"jquery": "3.3.1",
"jquery-validation": "1.17.0",
"jquery-validation-unobtrusive": "3.2.10",
"bootstrap": "3.3.7"
}
}
这将使NPM将在新的asp.net核心项目中使用的Bootstrap,JQuery和其他库下载到名为node_modules的文件夹中。下一步是将文件复制到适当的位置。为此,我们将使用gulp,该产品也由NPM下载。然后在项目的根目录中添加一个名为gulpfile.js的新文件。添加以下内容:
/// <binding AfterBuild='default' Clean='clean' />
/*
This file is the main entry point for defining Gulp tasks and using Gulp plugins.
Click here to learn more. http://go.microsoft.com/fwlink/?LinkId=518007
*/
var gulp = require('gulp');
var del = require('del');
var nodeRoot = './node_modules/';
var targetPath = './wwwroot/lib/';
gulp.task('clean', function () {
return del([targetPath + '/**/*']);
});
gulp.task('default', function () {
gulp.src(nodeRoot + "bootstrap/dist/js/*").pipe(gulp.dest(targetPath + "/bootstrap/dist/js"));
gulp.src(nodeRoot + "bootstrap/dist/css/*").pipe(gulp.dest(targetPath + "/bootstrap/dist/css"));
gulp.src(nodeRoot + "bootstrap/dist/fonts/*").pipe(gulp.dest(targetPath + "/bootstrap/dist/fonts"));
gulp.src(nodeRoot + "jquery/dist/jquery.js").pipe(gulp.dest(targetPath + "/jquery/dist"));
gulp.src(nodeRoot + "jquery/dist/jquery.min.js").pipe(gulp.dest(targetPath + "/jquery/dist"));
gulp.src(nodeRoot + "jquery/dist/jquery.min.map").pipe(gulp.dest(targetPath + "/jquery/dist"));
gulp.src(nodeRoot + "jquery-validation/dist/*.js").pipe(gulp.dest(targetPath + "/jquery-validation/dist"));
gulp.src(nodeRoot + "jquery-validation-unobtrusive/dist/*.js").pipe(gulp.dest(targetPath + "/jquery-validation-unobtrusive"));
});
此文件包含在构建和清理项目时执行的JavaScript代码。它将所有必要的文件复制到lib2(不是lib –您可以轻松更改此文件)。我在新项目中使用了相同的结构,但是很容易将文件更改到其他位置。如果您移动文件,请确保还更新_Layout.cshtml。请注意,在清理项目时,lib2-目录中的所有文件都将被删除。
如果右键单击gulpfile.js,则可以选择Task Runner Explorer。在这里,您可以手动运行gulp来复制或清除文件。
Gulp对于其他任务(例如缩小JavaScript和CSS文件)也可能很有用:
https://docs.microsoft.com/zh-cn/aspnet/core/client-side/using-gulp?view=aspnetcore-2.1
图书馆经理(简单)
右键单击您的项目,然后选择管理客户端库。文件libman.json现在打开。在此文件中,指定要使用的库和文件以及应将它们存储在本地的位置。真的很简单!以下文件复制了在创建新的ASP.NET Core 2.1项目时使用的默认库:
{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": [
{
"library": "jquery@3.3.1",
"files": [ "jquery.js", "jquery.min.map", "jquery.min.js" ],
"destination": "wwwroot/lib/jquery/dist/"
},
{
"library": "jquery-validate@1.17.0",
"files": [ "additional-methods.js", "additional-methods.min.js", "jquery.validate.js", "jquery.validate.min.js" ],
"destination": "wwwroot/lib/jquery-validation/dist/"
},
{
"library": "jquery-validation-unobtrusive@3.2.10",
"files": [ "jquery.validate.unobtrusive.js", "jquery.validate.unobtrusive.min.js" ],
"destination": "wwwroot/lib/jquery-validation-unobtrusive/"
},
{
"library": "twitter-bootstrap@3.3.7",
"files": [
"css/bootstrap.css",
"css/bootstrap.css.map",
"css/bootstrap.min.css",
"css/bootstrap.min.css.map",
"css/bootstrap-theme.css",
"css/bootstrap-theme.css.map",
"css/bootstrap-theme.min.css",
"css/bootstrap-theme.min.css.map",
"fonts/glyphicons-halflings-regular.eot",
"fonts/glyphicons-halflings-regular.svg",
"fonts/glyphicons-halflings-regular.ttf",
"fonts/glyphicons-halflings-regular.woff",
"fonts/glyphicons-halflings-regular.woff2",
"js/bootstrap.js",
"js/bootstrap.min.js",
"js/npm.js"
],
"destination": "wwwroot/lib/bootstrap/dist"
},
{
"library": "list.js@1.5.0",
"files": [ "list.js", "list.min.js" ],
"destination": "wwwroot/lib/listjs"
}
]
}
如果移动文件,请确保还更新_Layout.cshtml。