91

Visual Studio 2015 comes with built in support for tools like Grunt, Bower, Gulp and NPM for ASP.NET 5 projects.

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.

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.

Community
  • 1
  • 1
Robert Hegner
  • 9,014
  • 7
  • 62
  • 98

2 Answers2

129

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.

Step 1 (Create Project)

Create a new ASP.NET 4.5.2 Project (MVC Template) with Visual Studio 2015.

Step 2 (Remove Bundling/Optimization from Project)

Step 2.1

Uninstall the following Nuget Packages:

  • bootstrap
  • Microsoft.jQuery.Unobstrusive.Validation
  • jQuery.Validation
  • jQuery
  • Microsoft.AspNet.Web.Optimization
  • WebGrease
  • Antlr
  • Modernizr
  • Respond

Step 2.2

Remove App_Start\BundleConfig.cs from project.

Step 2.3

Remove

using System.Web.Optimization;

and

BundleConfig.RegisterBundles(BundleTable.Bundles);

from Global.asax.cs

Step 2.4

Remove

<add namespace="System.Web.Optimization"/>

from Views\Web.config

Step 2.5

Remove Assembly Bindings for System.Web.Optimization and WebGrease from Web.config

Step 3 (Add bower to Project)

Step 3.1

Add new package.json file to project (NPM configuration file item template)

Step 3.2

Add bower to devDependencies:

{
  "version": "1.0.0",
  "name": "ASP.NET",
  "private": true,
  "devDependencies": {
    "bower": "1.4.1"
  }
}

The bower package is automatically installed when package.json is saved.

Step 4 (Configure bower)

Step 4.1

Add new bower.json file to project (Bower Configuration file item template)

Step 4.2

Add bootstrap, jquery-validation-unobtrusive, modernizr and respond to dependencies:

{
  "name": "ASP.NET",
  "private": true,
  "dependencies": {
    "bootstrap": "*",
    "jquery-validation-unobtrusive": "*",
    "modernizr": "*",
    "respond": "*"
  }
}

These packages and their dependencies are automatically installed when bower.json is saved.

Step 5 (Modify Views\Shared\_Layout.cshtml)

Step 5.1

Replace

@Styles.Render("~/Content/css")

with

<link rel="stylesheet" href="~/wwwroot/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/Content/Site.css" />

Step 5.2

Replace

@Scripts.Render("~/bundles/modernizr")

with

<script src="~/wwwroot/lib/modernizr/modernizr.js" ></script>

Step 5.3

Replace

@Scripts.Render("~/bundles/jquery")

with

<script src="~/wwwroot/lib/jquery/dist/jquery.min.js"></script>

Step 5.4

Replace

@Scripts.Render("~/bundles/bootstrap")

with

<script src="~/wwwroot/lib/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="~/wwwroot/lib/respond/dest/respond.min.js"></script>

Step 6 (Modify other sources)

In all other Views replace

@Scripts.Render("~/bundles/jqueryval")

with

<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>

Useful Links


Bundling & Minifying

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.

Robert Hegner
  • 9,014
  • 7
  • 62
  • 98
  • 6
    Thank you VERY MUCH. Am I right that there is one step missed: how to map ~/wwwroot to /../bower_components (or gulp/grunt config how to move bower packages to "~/wwwroot" ) Could you add this step and describe how you would recommend to arrange the js/css code for running with IIS Express in VS2015 environment? – Roman Pokrovskij Aug 31 '15 at 09:56
  • When you create a `bower.json` file through Visual Studio 2015, it will automatically create a `bowerrc` file as well, which overrides the default location for bower installs from `bower_components` to `wwwroot/lib` – Sagebrush GIS Dec 01 '15 at 14:34
  • 1
    So it turns out bundling is quite useful to me. Now that you've removed the default bundler, I'd like to recommend [Bundler & Minifier](https://visualstudiogallery.msdn.microsoft.com/9ec27da7-e24b-4d56-8064-fd7e88ac1c40) to replace it. It uses the Task Runner Explorer and has a configuration file similar to npm and bower. It's a part of Web Essentials as well, so you might already have it installed. – LavaHot Jan 08 '16 at 06:14
  • 1
    [Here's](http://www.jeffreyfritz.com/2015/05/where-did-my-asp-net-bundles-go-in-asp-net-5/) a nice article on the topic to bundle with gulp. – LavaHot Jan 08 '16 at 06:22
  • @LavaHot thanks for these interesting links. I was so free as to include your links in my answer. – Robert Hegner Jan 08 '16 at 07:52
  • 1
    Thanks for the detailled info @SagebrushGIS! I'm also investigating on how to add Bower package management to my MVC project. I've added the bower.json via VS2015, but I don't see the bowerrc file you are talking about. Any steps I could be missing or where should I find this file? As a work around for now, I use: – Guido Kersten Aug 04 '16 at 19:04
  • bower_components and node_modules folders are not designed to be exposed by the web server. I recommend to create gulp task to copy the distributables files into the project's folder structure that is part of publishing process and your html paths need to be relative. – Roman Oct 20 '16 at 22:09
  • Fantastic, thanks i was looking for that. just an extension for the answer so you dont look around if you set behind the proxy and have ssl error. please follow this answer http://stackoverflow.com/questions/3777075/ssl-certificate-rejected-trying-to-access-github-over-https-behind-firewall – dori naji Nov 08 '16 at 12:21
4

It is actually not too different. It is just that there is better support for all these inside Visual Studio, for example when you add new items you have templates for bower or npm config files. Also you have templates for gulp or grunt configuration files.
But the actually calling of grunt/gulp tasks and binding them to build events is still done with Task Runner Explorer, just like in VS 2013.

Liviu Costea
  • 3,624
  • 14
  • 17
  • I guess I still don't see how you install npm packages in VS with the tool installed. Every time I try an .npm install it says my project isn't setup for node or whatever – Mastro Dec 18 '15 at 01:53
  • you need to have package.json created first in the root folder. I usually create that in the root of the target project as sibling of csproj file. then, you can use npm commands – Roman Oct 20 '16 at 22:10