After so much waiting, “I’ll do this first”, app coding etc. etc. (yep, I blame my family as well), I have finally begun playing around with DNX and ASP.NET 5.
For those of you who do not know the story, ASP.NET 5 will change everything…ish. You can create your web applications, web api:s and console apps on OS X and Linux, and run it everywhere. Nice, right?
Before I begin, I’ll just mention some terms that I’ll use in this post.
dnx stands for .NET Execution Environment. It’s a console application that you get access to when installing ASP.NET 5 on your Mac. You use it to start your web sites, run your console apps etc.
dnvm stands for .NET Version Manager.
dnu stands for DNX Utility. It’s a console application that you can use to install dependencies for your projects, much like npm.
I believe that this guide would work for Linux as well, but since I haven’t tried it, I won’t swear by it.
Step 1 – Install Mono
To get started with DNX, first go and grab Mono for your OS:
On this page, can download a convenient installer that sets everything up for you. Nice for those of you who like me, do not favor installing a bunch of bytes and having to build your own makefile.
Step 2 – Install Visual Studio Code
Visual Studio Code is an open source IDE (well, not quite yet), or rather a simple text editor that can be used to develop.NET projects. It was built with plugins in mind, however, so expect an explosion of functionality in the months to come.
You do not have to use Visual Studio Code when developing ASP.NET 5 stuff – you can use your editor of choice, like Sublime or Atom. However, I think that you should at least have a look at Visual Studio Code, to get your thumb on the pulse of the work carried out by the .NET 5 community.
Visual Studio Code can be downloaded as a convenient install bundle:
If you are used to the full feathered Visual Studio experience, you’ll notice that Visual Studio Code is insanely stripped. You cannot even create new projects or solutions from it. To do that, you need to setup ASP.NET 5.
Step 3 – Install ASP.NET 5
To install ASP.NET 5, download and install this file (but first check if an update is available, which probably is the case since so much is happening all the time):
Once you have installed ASP.NET 5, you have to set it up. Brace yourself – you will no longer rest in the comforting arms of install bundles. The rest of the setup will take place in the terminal.
Open up the terminal, then type dnx. As you’ll notice, the command will not be recognized. So, just run the following command:
source /Users/<your user name>/.dnx/dnvm/dnvm.sh
This will setup ASP.NET 5 and make it possible to run dnx and dnu from any folder. If you run the dnx command again, it will be recognized (although if naturally will warn you that you use it incorrectly).
Step 4 – (Optional) Install Yeoman
If you want to hack away in ASP.NET 5, you have all the tools you need now (and an optional Visual Studio Code). However, to simplify things even more, you can use Yeoman as a project generator, to avoid having to setup each project from scratch.
To install Yeoman and the ASP.NET project generator plugin, run these two commands in the terminal:
npm install -g yo
npm install -g generator-aspnet
Once installed, you can now create ASP.NET projects with Yeoman, using this command from any folder:
This will open a wizard that lets you choose from a selection of different project types. Once you have selected a project, Yeoman will generate it in a sub folder with the same name as your project.
Step 5 – Run your project
This post will not cover the project structure of ASP.NET 5 projects, but to sum it up, there are no solution files, project files are now small, pretty json files and project files hold no references to the files in the project. It’s a beautiful thing.
Once you have a project, cd into the project folder, then run dnu restore to restore all dependencies (like npm install).
Once all dependencies are restores, you can run your project. Depending on what kind of project you have, you run it in different ways:
- Console app – dnx run
- Web app – dnx web
- Web API – dnx web
- Test project – dnx test
Although you can run dnx build to build your project prior to running it, there is no need since the run commands builds the project as well.
That’s a wrap
That’s about it. In a future post, I’ll cover how to setup a code project with a test project connected to it.