ASP.NET Core Tutorial
Tags

ASP.NET Core MVC Linux Hello World Program

In this tutorial, we will create a simple hello world web mvc application in .NET Core.

.NET Core Installation on Linux

To install .NET on linux, we need to first add Microsoft package signing key to your trusted keys and add the package repository.

We have different commands for different linux distributions. Run below commands in terminal window according to your distribution.

Ubuntu
Add Repository wget https://packages.microsoft.com/config/ubuntu/21.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
SDK Installation sudo apt-get update
sudo apt-get install -y apt-transport-https
sudo apt-get update
sudo apt-get install -y dotnet-sdk-6.0
Debian
Add Repository wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
SDK Installation sudo apt-get update
sudo apt-get install -y apt-transport-https
sudo apt-get update
sudo apt-get install -y dotnet-sdk-6.0
openSUSE
Add Repository sudo zypper install libicu
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
wget https://packages.microsoft.com/config/opensuse/15/prod.repo
sudo mv prod.repo /etc/zypp/repos.d/microsoft-prod.repo
sudo chown root:root /etc/zypp/repos.d/microsoft-prod.repo
SDK Installation sudo zypper install dotnet-sdk-6.0
Fedora
SDK Installation sudo dnf install dotnet-sdk-6.0
RedHat Enterprise Linux (RHEL)
SDK Installation sudo dnf install dotnet-sdk-5.0
SLES
Add Repository sudo rpm -Uvh https://packages.microsoft.com/config/sles/15/packages-microsoft-prod.rpm
SDK Installation sudo zypper install dotnet-sdk-6.0

Check .Net Core Installed Versions

To check .net core installed version, run below command:


dotnet --info

dotnet_core_check_installation image

In the above screenshot, we can found out .NET core version numbers that are installed using above commands.

.NET SDK 6.0.200 is installed. .NET runtime version for AspNet Core is 6.0.2 and .NET Core app library version is 6.0.2.

New Project

To create a new project use below command


dotnet new mvc -lang C# -n FirstCoreSolution

dotnet core mvc new project

This will create a new folder name FirstCoreSolution in your file system. Use below commands to open your hello world project in Visual Studio code.

terminal open project

In Visual Studio code, we can see our first ASP.NET Core MVC project files are generated.

vscode-first-core-solution

Open Views -> Home -> Index.cshtml file, and update page with below text.


@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Hello ASP.NET Core MVC in Linux</h1>
</div>

Use below command to build the solution.


dotnet build

dotnet build command

After build is successfull, use below command to run application in the browser.


dotnet run

dotnet run comamnd

In the above screenshot, there are two urls. One with https and second one is http. By default, dotnet run command host your website in both urls. Open any one of the url in browser window.

first asp.net mvc core appCongratulations!!! You have created your first ASP.NET Core MVC web application in Linux operating system.