Using DotNet Core on Ubuntu to build an MVC website

I recently had some time to poke around outside of my normal comfort zone of open-source languages and try some new stuff from the Microsoft side of things. I was pleasantly surprised at how nice it is over the fence!

So the first thing I needed to do was install dotnet[1] and a dotnet-friendly editor[2].

I am on Ubuntu 16.04, so I had to add the repository to my aptitude source list.

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 417A0893
sudo apt-get update

It’s a little complicated, but it makes installing and maintaining versions that much easier. To install dotnet core, it was as easy as

sudo apt-get install dotnet-dev-1.0.1

Bam! dotnet core installed! Oh wait. Ok, so things didn’t go perfectly, and I had to do some manual installs to get it actually working.

sudo apt-get -f install
sudo apt-get install liburcu4 liblttng-ust0 dotnet-sharedframework-microsoft.netcore.app-1.1.1 dotnet-dev-1.0.1

Next I needed an editor, and I figured Visual Studio Code was the right choice. So I added the repository.

curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64] http://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'
sudo apt-get update

Which then made installing it just as simple.

sudo apt-get install code 

The next step was to get the environment set up to run C# application code nicely. So I opened Visual Studio Code and pressed Ctrl+Shift+P and typed ext install and chose to install the C# plugin from Microsoft.

Still with me? That’s it. The environment was really easy to set up. I decided to create a new MVC project from the command line (since I know it a lot better than my new editor) so I created a folder in my ~/src/ directory called the incredibly original dotnet_mvc.

mkdir -p ~/src/dotnet_mvc
cd ~/src/dotnet_mvc

To create a new default MVC project, use

dotnet new mvc

This will create essentially a “Hello World” type web application using Kestrel, a web server for ASP.NET.

The dependencies it has aren’t all automatically included, but a simple

dotnet restore

fixes those issues. (This is also managed by VS Code so if you do it in the IDE, there’s just a friendly popup asking you if you want to run the Restore action.)

Now we can actually start messing around inside the application! In Visual Studio Code, click File > Open Directory and navigate to the dotnet_mvc project folder. By default you have a HomeController.cs file inside the Controllers/ directory, and a Startup.cs file in the root which defines the server settings and url routes.

So proof of concept built, let’s see if it runs! Click the “Debug” icon on the left and press the little green arrow at the top of the screen. The project will be built and run, and VS Code will open your default browser to localhost:5000 where you will find a default web application.

dotnet_debug

Check back in next time when I add a new route, controller and support for a JSON REST API.

Advertisements
Using DotNet Core on Ubuntu to build an MVC website

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s