Visual Studio 2015 内置了对用于 ASP.NET 5 项目的 Grunt、Bower、Gulp 和 NPM 等工具的支持.
Visual Studio 2015 comes with built in support for tools like Grunt, Bower, Gulp and NPM for ASP.NET 5 projects.
但是,当我使用 Visual Studio 2015 创建 ASP.NET 4.5.2 项目时,它不使用这些工具.我想使用 bower 而不是 nuget 来管理客户端包.
However when I create a ASP.NET 4.5.2 project using Visual Studio 2015 it doesn't use these tools. I'd like to use bower instead of nuget to manage client side packages.
我可以找到有关在 Visual Studio 2013 中使用这些工具的信息(请参阅 this 问题为例).但我猜 Visual Studio 2015 的过程有所不同,因为它内置了对这些工具的支持.
I can find information about using these tools with Visual Studio 2013 (see this question for example). But I guess the procedure is different for Visual Studio 2015 since it has built in support for these tools.
虽然 Liviu Costea 的回答 是正确的,但它仍然我花了很长时间才弄清楚它是如何完成的.所以这是我从一个新的 ASP.NET 4.5.2 MVC 项目开始的分步指南.本指南包括使用 bower 进行客户端包管理,但(尚未)涵盖捆绑/grunt/gulp.
While Liviu Costea's answer is correct, it still took me quite some time to figure out how it is actually done. So here is my step-by-step guide starting from a new ASP.NET 4.5.2 MVC project. This guide includes client-side package management using bower but does not (yet) cover bundling/grunt/gulp.
使用 Visual Studio 2015 创建一个新的 ASP.NET 4.5.2 项目(MVC 模板).
Create a new ASP.NET 4.5.2 Project (MVC Template) with Visual Studio 2015.
卸载以下 Nuget 包:
Uninstall the following Nuget Packages:
从项目中删除 App_StartBundleConfig.cs
.
删除
using System.Web.Optimization;
和
BundleConfig.RegisterBundles(BundleTable.Bundles);
来自 Global.asax.cs
删除
<add namespace="System.Web.Optimization"/>
来自 ViewsWeb.config
从 Web.config
在项目中添加新的package.json
文件(NPM配置文件
项模板)
Add new package.json
file to project (NPM configuration file
item template)
将 bower
添加到 devDependencies
:
{
"version": "1.0.0",
"name": "ASP.NET",
"private": true,
"devDependencies": {
"bower": "1.4.1"
}
}
保存package.json
时会自动安装bower包.
The bower package is automatically installed when package.json
is saved.
在项目中添加新的bower.json
文件(Bower配置文件
项模板)
Add new bower.json
file to project (Bower Configuration file
item template)
将 bootstrap
、jquery-validation-unobtrusive
、modernizr
和 respond
添加到依赖项:
Add bootstrap
, jquery-validation-unobtrusive
, modernizr
and respond
to dependencies:
{
"name": "ASP.NET",
"private": true,
"dependencies": {
"bootstrap": "*",
"jquery-validation-unobtrusive": "*",
"modernizr": "*",
"respond": "*"
}
}
保存bower.json
时会自动安装这些包及其依赖项.
These packages and their dependencies are automatically installed when bower.json
is saved.
替换
@Styles.Render("~/Content/css")
与
<link rel="stylesheet" href="~/wwwroot/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/Content/Site.css" />
替换
@Scripts.Render("~/bundles/modernizr")
与
<script src="~/wwwroot/lib/modernizr/modernizr.js" ></script>
替换
@Scripts.Render("~/bundles/jquery")
与
<script src="~/wwwroot/lib/jquery/dist/jquery.min.js"></script>
替换
@Scripts.Render("~/bundles/bootstrap")
与
<script src="~/wwwroot/lib/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="~/wwwroot/lib/respond/dest/respond.min.js"></script>
在所有其他视图中替换
In all other Views replace
@Scripts.Render("~/bundles/jqueryval")
与
<script src="~/wwwroot/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/wwwroot/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
在下面的评论中 LavaHot 推荐 Bundler &缩小器扩展 作为我在步骤 2 中删除的默认捆绑器的替代品.他还推荐 这篇文章关于与 Gulp 捆绑.
In the comments below LavaHot recommends the Bundler & Minifier extension as a replacement for the default bundler which I remove in step 2. He also recommends this article on bundling with Gulp.
这篇关于将 Grunt、Bower、Gulp、NPM 与 Visual Studio 2015 一起用于 ASP.NET 4.5 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!