我正在使用npm来管理我的ASP.NET核心应用程序所需的jQuery,Bootstrap,Font Awesome和类似的客户端库.
对我有用的方法首先将package.json文件添加到项目中,如下所示:
{
"version": "1.0.0",
"name": "myapp",
"private": true,
"devDependencies": {
},
"dependencies": {
"bootstrap": "^3.3.6",
"font-awesome": "^4.6.1",
"jquery": "^2.2.3"
}
}
Run Code Online (Sandbox Code Playgroud)
npm将这些包恢复到node_modules文件夹中,该文件夹与项目目录中的wwwroot处于同一级别:
由于ASP.NET Core提供了来自wwwroot文件夹的静态文件,并且node_modules不在那里,我不得不进行一些更改才能完成这项工作,第一个:在app.UseStaticFiles之前添加app.UseFileServer. cs文件:
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"node_modules")),
RequestPath = new PathString("/node_modules"),
EnableDirectoryBrowsing = true
});
app.UseStaticFiles();
Run Code Online (Sandbox Code Playgroud)
第二个,包括project.json文件中的publishOptions中的node_modules:
"publishOptions": {
"include": [
"web.config",
"wwwroot",
"Views",
"node_modules"
]
},
Run Code Online (Sandbox Code Playgroud)
这适用于我的开发环境,当我将它部署到我的Azure App Service实例时,它也可以工作,jquery,bootstrap和font-awesome静态文件得到很好的服务,但我不确定这个实现.
这样做的正确方法是什么?
这个解决方案是在从多个来源收集大量信息并尝试一些不起作用之后得出的,并且从wwwroot外部提供这些文件似乎有点奇怪.
任何建议将不胜感激.