If vNext or .Net 5 sounds new to you (which of course I believe it is not) or you want to read about the basics of these then please go through the following articles that I posted earlier:
Install Visual Studio 2015 Ultimate Preview
I recommend that you install VS2015 to explore all these utilities and the awesome features provided with ASP.Net 5. To read more about the installation of VS2015, please go through my article: Setup Visual Studio 2015 Preview Quickly Via Azure.
(This was also chosen by ASP.Net as the article of the day.)
ASP.Net 5 Redesign
ASP.Net 5 has undergone a major redesign from existing frameworks. Now we have three choices for choosing the framework:
Let's look at these runtime environments.
- Create a new ASP.Net Web Application.
- Select ASP.NET 5 Starter Web
Click OK and the project structure is ready. The project structure contains MVC folders and configuration files. Let's have a look at these configuration files.
- Project.json. The main project file lists the dependencies, frameworks, modules and so on needed for your application.
Traverse the following link for more details on Project.json: https://github.com/aspnet/Home/wiki/Project.json-file
- package.json. Lists npm packages.
- bower.json. Lists Bower packages.
- gruntfile.js. Configures Grunt tasks.
wwwroot folder
This promotes a separation of code, since your code behind files are separate from the static client files. This new wwwroot folder contains assets that the app will serve directly to clients, including HTML, CSS, image and JavaScript.
Choice of framework
Now that we have an idea of the folder structure and files, it's time to see the effect of these new configuration files:
- Go to the Project.json file and you'll find two frameworks:
- aspnet50: The full .NET CLR is the default runtime for projects in Visual Studio.
- aspnetcore50: The Core CLR is a lean and completely modular runtime for ASP.NET 5 projects. It's 11MB vis-a-vis 200MB size of the full .NET CLR.
You can select / switch among frameworks after selecting Project > Properties…
Other tools/utilities:
These new configuration files use other platform/tools also, let's understand them.
Node.js® is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
Definition by: NodeJS.
You can install Node.Js from the site or I prefer using: Chocolatey.
- Install chocolatey using the following command.
- @powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin.
This is equivalent to sudo-apt-get command in UNIX.
- choco install nodejs.
- After node is installed, you can validate whether npm (the package manager) is installed successfully or not.
npm --version
Bower, manages all these things for you. The web sites are made of many things, frameworks, libraries, assets, utilities and rainbows.
Install bower
npm install -g bower
Grunt is the JS taskrunner. The need to use Grunt is to do automation, it helps in automating many tasks, like minification, compilation, unit testing, linting, and so on, the easier your job becomes. If you visit the site you'll find many plugins available: Gruntjs.
Run the following command to install Grunt.
- npm install -g grunt-cli
Great!! You've installed the various tools now. Let's revisit the project we've created and understand the structure.
Demo of bowerIf you want to install any dependency that you want to use in your project, for example AngularJS, then go to bower.json and add AngularJS. For example:
The full code will look like:
Now, we've given the reference, you can view this by expanding Solution Explorer Dependencies > Bower.
It says that AngularJS is not installed. There are two ways to install AngularJS in the project:
- Right-click and fire the command Restore Package.
- Go to the command prompt and run the bower update.
The output of both commands will add AngularJS to your project.
Demo of Grunt
As we know Grunt is a taskrunner and this demo will show you the task grunt-contrib-copy.
- Go to package.json and add the NPN grunt package grunt-contrib-copy.
- Expand Dependencies and you'll reference to this npm package.
- Right-click grunt-contrib-copy and Restore package.
or
- Go to a command prompt and run the command:
npm install grunt-contrib-copy.
Good, now this plugin is available in your project. You can read more details regarding this plugin at: grunt-contrib-copy.
- Create a folder library and add custom.js as in the following:
- console.log('hello VS2015');
- Now let's leverage this plugin. Add the task to gruntfile.js
- copy: {
- main: {
- src: ‘library/custom.js',
- dest: 'wwwroot/',
- },
- },
- Register this task in gruntfile.js.
- grunt.loadNpmTasks('grunt-contrib-copy');
The final gruntfile.js will look like this:
-
-
-
- module.exports = function (grunt) {
- grunt.initConfig({
- copy: {
- main: {
- src: 'library/custom.js',
- dest: 'wwwroot/',
- },
- },
- bower: {
- install: {
- options: {
- targetDir: "wwwroot/lib",
- layout: "byComponent",
- cleanTargetDir: false
- }
- }
- }
- });
-
-
- grunt.registerTask("default", ["bower:install"]);
-
-
-
- grunt.loadNpmTasks("grunt-bower-task");
- grunt.loadNpmTasks('grunt-contrib-copy');
-
- };
We're all set to use this plugin. You can leverage VS2015.
- Right-click on gruntfile.js and run Task Runner Explorer.
You'll notice there are two tasks now:
Bower the default one provided by scaffold and copy that that we created in gruntfile.js. Double-click on the copy task to run it and it'll copy the Library>Custom.js file to the destination folder wwwrrroot.
- You can do the same operation via command prompt by firing grunt copy.
Validate the tree structure or look into Windows Explorer and you'll find the following pattern:
Summary
ASP.Net 5.0 is really powerful and has capabilities of other tools and frameworks too. We've unleashed the power of NPM, bower and grunt. There are many Grunt plugins available that make your life easy, for examle the common tasks linting, minification and obfuscation can be handled easily via Grunt plugins.