Just a few years ago, Microsoft was decidedly anti-Linux.
Mono – the cross-platform, open-source version of the .NET Framework – had been around since 2004. But it was independent of Microsoft, was always playing catchup, and not widely accepted.
So in 2014, Microsoft surprised everyone by announcing a new, cross-platform .NET!
.NET Core runs on Windows, Linux, and macOS. It’s a fairly large subset of the full .NET Framework, provides excellent performance, and is very much geared towards web applications using ASP.NET Core.
What We’ll Be Doing
On a Linux machine (Ubuntu 16.04) we’ll be installing .NET Core, and creating and running an ASP.NET Core web application.
Then we’ll install Visual Studio Code, a freely available source code editor (*) from Microsoft, and use it to build and debug a .NET Core web application.
(*) Visual Studio – which has been in existence since the 1990s – is Windows only, and paid software. Visual Studio Code, on the other hand, has only been around since 2015, and is cross-platform.
Right, let’s go…
Open up a terminal, and enter these four commands:
sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list' sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys B02C46DF417A0893 sudo apt-get update sudo apt-get install dotnet-dev-1.0.4
Refer to https://www.microsoft.com/net/core#linuxubuntu for full details.
Creating a .NET Core Project
In the terminal, enter:
cd mkdir dev cd dev dotnet new mvc --name webapp1
The dotnet command will take a minute or so while it creates a local cache “to improve restore speed and enable offline access”.
cd webapp1 dotnet restore dotnet build dotnet run
This will output text similar to the following:
Hosting environment: Production
Content root path: /home/grahamb/dev/webapp1
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Open up a browser, and navigate to http://localhost:5000:
Return to the terminal, and press Ctrl+C to terminate the process.
Visual Studio Code
Go to https://code.visualstudio.com/Download and click the .deb box below Tux (the Linux penguin) to download the package.
In the terminal, enter:
sudo dpkg --install ~/Downloads/code_1.14.2-1500506907_amd64.deb
and wait for the installation to complete (note that the version number in the filename might be different for your download!).
Create a .NET Core Project
Return to the terminal, and enter:
cd ~/dev dotnet new mvc --name webapp2 cd webapp2 code .
This opens Visual Studio Code in the current directory (~/dev/webapp2):
A new web browser window will also be opened, showing a Getting Started page for Visual Studio Code. Feel free to browse this documentation.
Double-click Program.cs or Startup.cs. You’ll see an information bar at the top; click Show Recommendations:
Locate the C# extension in the list, click Install, wait a few seconds, click Reload, click Reload Window.
The Output pane will automatically open, and the C# dependencies will be updated.
Click Yes and Restore in response to the two messages:
Wait for the NuGet (*) packages to be restored:
(*) What’s NuGet, some of you might ask? It’s a package manager originally created for Visual Studio, similar to npm (Node.js) and pip (Python).
Now click the Debug icon on the left:
Click the dropdown beside the green debug icon at the top, and select “.NET Core Launch (web)”:
Now click the green debug icon (or press F5). Debugging will start and a browser window will automatically open:
Return to Visual Studio Code and click the red stop icon in the toolbar.
As an exercise, I’ll let you experiment with debugging and stepping through the source code. With the cursor on a line – try the actions in HomeController.cs – press F9 to set a breakpoint. When you navigate to the page in the web browser, program execution will halt at the breakpoint. Press F10 to execute the current statement, or F5 to continue execution. If you’re a Visual Studio user, you’ll already be familiar with these keyboard shortcuts!