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 the Microsoft package signing key to your trusted keys and add the package repository.

We have different commands for different Linux distributions. Run the below commands in the 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 the.net core installed version, run the below command:

dotnet --info
dotnet_core_check_installation image

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

.NET SDK 6.0.200 is installed. The.NET runtime version for AspNet Core is 6.0.2, and the.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 named FirstCoreSolution in your file system. Use the 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 the page with the below text.

@{ 
    ViewData["Title"] = "Home Page"; 
} 
<div class="text-center">
    <h1 class="display-4">Hello ASP.NET Core MVC in Linux</h1>
</div>

Use the below command to build the solution.

dotnet build
dotnet build command

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

dotnet run
dotnet run comamnd

In the above screenshot, there are two URLs. One with HTTPS, and the second is HTTP. By default, dotnet run commands host your website in both URLs. Open any one of the urls in the browser window.

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.