Site icon Cloud | Mobile | Edge

Creating an ASP.NET Core application for Raspberry Pi

As a part of the Wren Hyperion solution, an ASP.NET Core application will run on an ARM based Linux OS (we are building a POC for the Raspberry Pi and Raspian).  Here are the steps on how you can get started with ASP.NET Core on Raspian:

Setup a Raspberry Pi with Raspian

Follow the guides on the Raspberry Pi website to install Raspian on your Pi.

Install .NET Core

  1. Download and Install

    To start building .NET apps you just need to download and install the .NET SDK (Software Development Kit) for Windows.

    Download .NET SDK

    Video: Creating a Simple .NET Application on Windows

  2. Get an editor

    Visual Studio is a fully-featured integrated development environment (IDE) for developing .NET apps on Windows.

    Download Visual Studio

    Select the .NET Core cross-platform development workload during installation:

  3. More info

    While you wait for Visual Studio to install, you can keep learning with the .NET Quick Starts. In the first Quick Start you’ll learn about collections.

    Quick start: Collections

    Once Visual Studio is installed, come back and build your first .NET app using Visual Studio.

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
  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 .
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\ubuntu.16.04-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.

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 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.

Exit mobile version