In this tutorial I will explain how to install a .NET Core MVC application on a Raspberry Pi running Raspbian for the OS. I am going to use Windows 7 as the developer machine but the same process will also work on Windows 10. We will then compile it targeting ARM for the Raspberry Pi.

Setting up the Raspberry Pi

I am not going to go into details on how to get Raspbian installed and how you shell into a terminal on a linux box as that is covered on many other posts. To run .Net Core MVC on Raspbian the first thing is to install .Net Core. On the Raspberry Pi in a terminal window run the following commands.

First we will update the Raspbian installation by running the following command.

sudo apt-get -y update 

.Net Core needs some packages installed to function properly, so let’s install those next.

sudo apt-get install curl libunwind8 gettext apt-transport-https 

Let's download the latest version of .Net Core 2.

wget https://dotnetcli.blob.core.windows.net/dotnet/Runtime/release/2.0.0/dotnet-runtime-latest-linux-arm.tar.gz 

Now we need to unpack the archive file so let’s first create a folder for it.

sudo mkdir /opt/dotnet 

Unzip the file into the folder we just created

sudo tar -xvf dotnet-runtime-latest-linux-arm.tar.gz -C /opt/dotnet 

Now that all the files are installed we need to create a symbolic link to the directory on the path so that we can call dotnet

sudo ln -s /opt/dotnet/dotnet /usr/local/bin

I always like to clean after an installation, so let’s delete the file we downloaded.

sudo rm dotnet-runtime-latest-linux-arm.tar.gz

At this point you should be able to test the install by executing the following in a terminal window on the Raspberry Pi:

dotnet --info

As you can see getting the .Net Core 2 on your Raspberry Pi is pretty easy, now lets create the MVC application.

ASP.Net Core 2.0 Hello World Web Application

We will create this project on our developer machine and target the ARM processor for deployment to a Raspberry Pi. This tutorial will only cover doing this on a Windows machine.

To make sure you have .Net Core 2.0 SDK installed on your development machine, open up a command prompt and execute the following:

dotnet --info

You should see something like the following output:

Create the Web App

Back on your developer workstation in that CMD prompt, create a new directory to hold your web application.

mkdir hello-world  

Then jump into that new directory.

cd hello-world  

Then create a new web application.

dotnet new web  

This created a simple web application that we can test on our developer workstation to make sure it works.

Execute the following command.

dotnet run

You should see something like the following output:

Now you can open up a browser and navigate to http://localhost:5000 You should see something like the following:

Ok this is pretty bare bones but I did say we were going to create a hello world web application. But wait the real fun part is getting this to run on the Raspberry Pi. Go ahead and kill the development web server in the CMD prompt using Ctrl+C. First we need to change the code so that it will bind to more IP addresses then the localhost one. Edit the Program.cs class to look like this: We are just adding line 4

 public static IWebHost BuildWebHost(string[] args) =>
      WebHost.CreateDefaultBuilder(args)
          .UseStartup()
          .UseUrls("http://*:5100")
          .Build();

Now lets publish this web application to a linux ARM platform.

dotnet publish -c Release -r linux-arm   

The output of this commend looks something like this:

This command generated all the files you need to copy over to the Raspberry Pi. This is where you need to figure out the best way to copy the files to the Raspberry Pi. I like to use WinSCP. It can be downloaded from here: https://winscp.net/eng/download.php

On the Raspberry Pi create a folder called hello-world. Copy all of the files in the bin\Debug\netcoreapp2.0\linux-arm\publish folder from you production computer to the hello-world folder on the Pi.

Now we can run our application like this:

dotnet ./hello-world/hello-world.dll   

Your application is now running and as you can see it is listening on port 5100.

You can now open any browser on your network and type in http://:5100/ it should look like this:

Run the app as a service

If we want our app to be always running, restarting it manually every time it crashes or when the Raspberry Pi reboots isn’t going to be sustainable… What we want is to run it as a service, so that it starts when the system starts, and is automatically restarted if it stops working. To do this, we’ll take advantage of systemd, which manages services in most Linux distros, including Raspbian.

To create a systemd service, create a MyWebApp.service file in the /lib/systemd/system/ directory, with the following content:

 [Unit]
 Description=My ASP.NET Core Web App
 After=nginx.service
 
 [Service]
 Type=simple
 User=pi
 ExecStart=/opt/dotnet/dotnet /home/pi/hello-world/hello-world.dll
 Restart=always
 
 [Install]
 WantedBy=multi-user.target

Enable the service like this:

sudo systemctl enable MyWebApp 

And start it like this (new services aren’t started automatically):

sudo systemctl start MyWebApp

And that’s it, your app is now monitored by systemd, which will take care of starting or restarting it as needed.

Conclusion

As you can see it is pretty easy to create a web application on your development machine and target the linux-arm platform so that the application can be copied over to the Raspberry Pi and executed.