# Creating an ASP.NET Core application for Raspberry Pi

https://jaredrhodes.com/blog/creating-an-asp-net-core-application-for-raspberry-pi/

As a part of the Wren Hyperion solution, an [ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/) application will run on an ARM based Linux OS (we are building a POC for the [Raspberry Pi](http://raspberrypi.org/) and [Raspian](http://raspbian.org)). Here are the steps on how you can get started with ASP.NET Core on Raspian:

- [Setup a Raspberry Pi with Raspian](#setup-a-raspberry-pi-with-raspian)
- Install .NET Core
- Create your ASP.NET Core solution
- Publish to the Raspberry Pi
- Install .NET Core to the Raspberry Pi
- Run your application on the Raspberry Pi

## Setup a Raspberry Pi with Raspian

Follow the [guides on the Raspberry Pi website](https://www.raspberrypi.org/documentation/installation/installing-images/) to install Raspian on your Pi.

## Install .NET Core

This post targets the .NET Core 2.x era. For current SDK installation steps on Windows, Linux, or ARM devices, follow the official guide: [Install .NET SDK, .NET runtime, and tools](https://learn.microsoft.com/en-us/dotnet/core/install/).

## Create your ASP.NET Core Solution

1. Create a new .NET Core project.

    On macOS and Linux, open a terminal window. On Windows, open a command prompt.

    ```
    dotnet new razor -o aspnetcoreapp
    ```

2. Run the app.Use the following commands to run the app:

    ```
    cd aspnetcoreapp
    dotnet run

    ```

3. Browse to [http://localhost:5000](http://localhost:5000/)

4. Open *Pages/About.cshtml* and modify the page to display the message "Hello, world! The time on the server is @DateTime.Now ":

    ```
    @page
    @model AboutModel
    @{
        ViewData["Title"] = "About";
    }
    <h2>@ViewData["Title"]</h2>
    <h3>@Model.Message</h3>

    <p>Hello, world! The time on the server is @DateTime.Now</p>

    ```

5. Browse to <http://localhost:5000/About> and verify the changes.

## Publish to the Raspberry Pi

On macOS and Linux, open a terminal window. On Windows, open a command prompt and run:

`dotnet clean .`, `dotnet restore .`, and `dotnet build .`

This will rebuild the solution. Once that is complete run the following command:

`dotnet publish . -r linux-arm`

This will generate all the files needed for running the solution on the Raspberry Pi. After this command completes generating the needed files, the files need to be deployed to the Pi. Run the following command in Powershell from the publish folder:

`& pscp.exe -r .\bin\Debug\netcoreapp2.0\linux-arm\publish\* ${username}@${ip}:${destination}`

Where username is the username for the Pi (default "pi"), ip is the ip address of the Pi, and destination is the folder the files will be published to.
## Install .NET Core to the Raspberry Pi

This section is sourced from [Dave the Engineer's post on the Microsoft blog website](https://blogs.msdn.microsoft.com/david/2017/07/20/setting_up_raspian_and_dotnet_core_2_0_on_a_raspberry_pi/).

The following commands need to be run on the Raspberry Pi whilst connected over an SSH session or via a terminal in the PIXEL desktop environment.

- Run **sudo apt-get install curl libunwind8 gettext**. This will use the apt-get package manager to install three prerequiste packages.
- Run **curl -sSL -o dotnet.tar.gz https://dotnetcli.blob.core.windows.net/dotnet/Runtime/release/2.0.0/dotnet-runtime-latest-linux-arm.tar.gz** to download the latest .NET Core Runtime for ARM32. *This is refereed to as **armhf** on the [Daily Builds](https://github.com/dotnet/core-setup#daily-builds) page.*
- Run **sudo mkdir -p /opt/dotnet &amp;&amp; sudo tar zxf dotnet.tar.gz -C /opt/dotnet** to create a destination folder and extract the downloaded package into it.
- Run **sudo ln -s /opt/dotnet/dotnet /usr/local/bin** to set up a symbolic link...a shortcut to you Windows folks  to the **dotnet** executable.
- Test the installation by typing **dotnet -help**.

- Try to create a new .NET Core project by typing **dotnet new console**. Note this will prompt you to install the .NET Core SDK however this link won't work for Raspian on ARM32. This is expected behaviour.

## Run your application on the Raspberry Pi

Finally to run your application on the Raspberry Pi, navigate to the folder where the application was published and run the following command:

`dotnet run`

You can then navigate to the website hosted by the Raspberry Pi by navigating to http://{your IP address here}:5000.
