In this post I am going to add a new project to the my existing ASP.NET Core Basics solution which can be found in this repository. The new project will be MVC 6 to which I will add in Aurelia. With both ASP.NET and Aurelia now being at RTM I thought this would be a good time to cover getting a new project setup.
Over time the ASP.NET Core Basics repo used in this post is going to be replacing my ASP.NET Core SPAs repo based on some feedback that having Aurelia and Angular 2 in the same project made it harder to see how each individual framework is setup.
Part 1 – Add Aurelia to an ASP.NET Core Project (This Post)
Part 2 – Aurelia with an ASP.NET Core API
Part 3 – Aurelia with an ASP.NET Core API: Modeling and Displaying Data in Aurelia
Part 4 – Aurelia with ASP.NET Core: Host Aurelia from a Controller
Github repo with the code for all of the parts with a release to go with each post
Adding a new project to an existing solution
To add a new project to the existing solution right click on the solution and then click Add > New Project.
On the Add New Project dialog select ASP.NET Core Web Application (.NET Core), enter a name and then click OK.
On the New ASP.NET Core Web Application (.NET Core) dialog select Web Application. This application doesn’t need authentication so leave it set to No Authentication. Finally click OK.
After a few seconds the project creation will complete and the solution will contain two projects. The existing Contacts project that contains both a razor/normal implementation of a contacts list as well as an API implementation. The second project is the newly created Aurelia project.
Changing the startup project
Notice in the screenshot above that the Contacts project is in bold. This means that the Contacts project is set as the startup project and it will be the project that starts when the application is run (F5 or Cntrl + F5). In this post we will just be working with the Aurelia project so we need to make it the startup project. To do this right click the Aurelia project and select Set as StartUp Project.
Now if you hit F5 the Aurelia project will run. Visual Studio provides a lot of flexibility around which projects start up. You can select a single, have which ever project you have to have select, or even multiple projects. In a later post we will need both projects to start up and I will cover that when we have the need.
Install the Aurelia CLI
Make sure you have a minimum of NodeJs 4.x or above installed. If you need the installer it can be found here. After the install is complete open a command prompt and run the following command to install the Aurelia CLI.
npm install aurelia-cli -g
Add Aurelia to existing ASP.NET Core Project
In a command prompt navigate to the folder that contains the xproj file for the ASP.NET Core project created above. Now the Aurelia CLI can be used to setup a new Aurelia project at the current location using the following command.
au new --here
There will be a series of prompts the first of which is the selection of which platform to use. Select the option for ASP.NET Core (option 2). I used the defaults for most of the remaining prompts. The exception was for unit testing which I selected no on just to keep the project simpler not because I think testing is a bad idea.
When the Aurelia CLI finishes its file creation and dependency restore your project will contain the highlighted new files and folders.
Notice that I have a warning on Dependencies that something is not installed. There is a quirky issue with Visual Studio that Scott Hanselman has blogged about here. He goes in to a good bit of detail about what is going on as well as suggesting a work around. It has to do with npm and not being about to restore an optional package that isn’t meant for Windows machines.
gulp
The Aurelia CLI creates a set of tasks to help with building, transpiling the Aurelia part of the applications. I wrote a couple of posts over the couple few weeks dealing with converting a project to use glup as well as how to get gulp working with ES 2015.
I am going to cover the abbreviated version of those two post here. Add a new file called gulpfile.babel.js in the root of the project, where your project.json is located. The Aurelia CLI added all the needed items in the devDependencies section of package.json.
gulp no go
At this point I attempted to include the tasks under aurelia_project/tasks using require(‘require-dir’)(‘aurelia_project/tasks’);. This failed completely. I couldn’t get any of the items in the tasks folder to show up. I am not sure why this didn’t work. My best guess is that the tasks in the tasks folder are exporting gulp.series and not gulp.task. I just don’t know enough about gulp at this point to now how to fix/work around this or if what I am trying to do is just not the right way it should be used.
The gulp work around
I spent more time that I would have like working on getting gulp to pick up the items in the tasks folder, but I don’t want to have to run a CLI command every time I do a build to make sure all the Aurelia related files are up to date. As a work around I decided to add a gulp task to invoke the CLI command for me.
To start open package.json and add the following to the devDependencies section which allows shell commands to be run from gulp.
"gulp-shell" : "0.3.0"
Next in gulpfile.babel.js added the proper imports and created tasks for the CLI commands I wanted to run. In the case I am just showing the build command.
import gulp from 'gulp'; import shell from 'gulp-shell'; gulp.task('bulid', shell.task(['au build']));
Using the Task Runner Explorer this task can now be set to run after a build of the MVC project.
This accomplishes what I wanted, but it feels like a hack. If anyone knows a better way please let me know.
It’s Alive!
At this point if you run the application it will go to the normal default home page that gets created by the Visual Studio template. For me that address is http://localhost:37472/. From there if you add index.html, the full address is http://localhost:37472/index.html, you will be invoking the Aurelia application.
At this point all you will see is “Hello World!”. Not that impressive I know, but it is a starting point that we will build on in future posts.
The associated code can be found here.
Thanks for this. Have you found a better way to marry .NET Core with the Aurelia CLI?
At the moment I haven’t come up with anything better.
Can you still use au run –watch with this approach?
I haven’t tried the watch command. If watch also starts the application then it would be by passing the MVC application, which is fine for this example, but if the app as hosted from MVC it could cause problems.
Thanks! This helped a lot, especially your bridge from gulp to aurelia-cli to get Visual Studio’s Task Runner Explorer connected.
Thanks for the feedback Jason! You should also check out JavaScript Services now that it has integration with Aurelia. It is a great way to get up and running with Aurelia and webpack. Here is a post I did on creating an Angular 2 project with JavaScript Services that should give you the idea of how it works. I had one small issue with the Aurelia project created with JavaScript Services, but I documented the fix here.
Thanks for publishing this very helpful example. When I build the solution found in “ASP.NET-Core-Basics-0.4.0.zip”, no matter what I do, it does not execute “au build”. I am just doing a Visual Studio “build” of the solution. What am I doing wrong?
In my application, I am using TypeScript. What do recommend for the file name of “gulpfile.babel.js”. Would it be “gulpfile.typescript.ts”?
Also, In your file “gulpfile.babel.js” you have a type of “bulid” in “`guilp.task(‘bulid’, shell.task([‘au build’]));“` Don’t know if it matters. — Paul
Paul, the first thing I would do is open the task runner explorer and verify that you have a binding on the “build” task set to After Build. To check right click on the task and expand Bindings and After Build should be checked.
Next, verify that if you run the task manually that it executes the au build.
Keep the gulpfile.babel.js and it will still work fine with your project. Unless something has changed I don’t believe having a gulpfile.babel.ts is supported.
In the gulpfile “build” is the name of the task which is using gulp-shell to execute the command “au build” in the shell.
Any chance you could make an updated version of this? I cant figure out what is supposed to work “out of the box” since VS 17 RC is changing constantly and maybe Aurelias core support is too
Mads, I can add it to the list of things to do. To keep you from having to wait I would recommend that you check out JavaScript Services the Yeoman generators it provides give the option to chose between project.json and csproj.
Year I have been watching https://www.youtube.com/watch?v=wQaAACHj7w8 which uses Yeoman, think I just skipped the part yesterday where I could choose between those, so I will take a look at that tonight :)
Easy to miss something when going through all those prompts. Let me know if you have any trouble and I will see what I can do to help the issue. Good luck Mads!