Community Post

Cross platform ASP.NET core: Part 1- writing your first .NET core application.

Temi Lajumoke

Introduction

This article is the first of a series of articles to guide you towards building your first ASP.NET core application from scratch on either Windows, Linux or MacOS. As you go through this article, and subsequent articles in this series, you'll learn about the fundamentals and design patterns of ASP.NET core. You'll also be able to build your very own ASP.NET core web application, complete with routing, logging, the MVC architecture, Web APIs, authentication and many more berries in the box.

ASP.Net core is a relatively new, cross-platform, portable, and open source web framework developed by Microsoft, for building modern cloud-based Internet-connected applications, such as web apps, Internet of things (IoT) apps and mobile backends.

ASP.NET core is modeled after the robust and powerful ASP.NET framework but built from the ground up in a modular format. It's also one of the fastest web frameworks on the planet.

In this article, we are going to build our first dotnet core console application using C#. If you’re new to .NET development, and would love to start using ASP.NET Core in no time, welcome!

Prerequisites

This series assumes that you have some knowledge of programming. While knowledge of programming with C# will be valuable to you, I reckon you can follow through and do just fine without it.

I use visual studio code as my code editor because of it's robust support for debugging .Net core applications. However, any modern text editor or IDE you choose should work fine.

Installing .NET core

To develop and run our .NET core application, we'd first need to install the .NET core SDK on our local machine. You can download the SDK by visiting the .NET webpage, which (depending on the time you're reading this) should look something similar to this. Make sure to download the right framework for your OS version; so If you're on windows or linux, you should look out for the SDK that supports your Windows architecture or Linux flavour (for instance Windows(x64), Ubuntu 14, Ubuntu 16, Debian).

Setting up

Once you've successfully downloaded and installed the .NET core SDK, you can verify you have the dotnet runtime enviroment by firing up your terminal or command prompt and typing in dotnet. Like so

dotnet 

If you have .NET core installed correctly, it should set up your local package cache (on your first run) and then display some information about .NET core as shown below. Eureka! You're all set to start building your .NET core first application.

Creating your first application

You get started creating your application by creating a folder and naming it as you please. .NET core automatically names your project after the name of your directory or folder. Next, you create a new .NET core project inside the directory, like so.

dotnet new console

This command generates two files for you :

  • Program.cs - C# file which contains the entry point into the application.
using System;

namespace cross_platform_asp_net_core
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
  • <FOLDER_NAME>.csproj xml-based file which contains the framework and all necessary dependencies needed for our application to compile and run. Dependencies are simply modules or nuget packages that our application depends on . We'll look at them in detail as we flesh out our application.
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

</Project>

Next, we install the dependencies our application needs by running the following command.

dotnet restore

and now you are ready to run your application.
Running a .NET core application is pretty intuitive and simple, all you have to do is run the following in your command line.

dotnet run

If you followed all steps correctly, your application should compile and run without any problems, like so.

And you've just successfully created your first .NET core application, using only three commands. dotnet new, dotnet restore & dotnet run. Pretty neat right?

The .Net core template application is a simple C# console "Hello World" application. The template automatically defines a class called Program in the namespace cross_platform_asp_net_core. The Program class contains a single method, called Main, which is defined with string array (string[]) parameter. The Main method is the entry point of a C# console application. It is invoked as soon as the application is started. The Main Method calls the WriteLine() method in the Console class to display the literal string "Hello World!.

You might be thinking: wait, Namespace? Class? What are these!?. Let's break it down a little.

  • A Namespace is simply a wrapper that is used to group or organize classes. Namespaces help you control the scope of class and method names.
  • A Class is a group of related variables and methods. Classes enable you create your own custom types by grouping together variables of other types and methods.
  • A Method is basically a block of code that contains one or more statements. Your application program causes the statements to be executed by invoking the method.

You can learn more about these by going through the dotnet documentation.

Enhancing our first application

We can enhance our application to prompt the user for their name and display it along with a custom message, by replacing our Main method with the following code.

static void Main(string[] args)
{
    Console.WriteLine("Please enter your name");            
    var name = Console.ReadLine();
    Console.WriteLine($"Hello {name}!, loving .NET core already, aren't ya?");
}
  • The first line of code in the Main method tells the user to enter their name.
  • The second line waits until the user enters a string and presses the Enter key. It then reads the string and stores it into a variable named name.
  • The third line prints to the console, the name supplied by the user, coupled with our custom message.
  • The dollar sign ($) tells our C# compiler that the word inside the curly braces is a special kind of string, as a result, the C# compiler knows to pass in the value of the name variable into the placeholder. This is known as string interpolation.

Next Steps

We've just seen how easy it is to run .Net core applications on any operating system, and maybe you've even written your first one. Well done! However, we've only built a console app, and not a webapp. We'll do that next, as we learn how to run our .Net core application on the web, and send data to the browser.