left-icon

ASP.NET Core Succinctly®
by Simone Chiaretta and Ugo Lattanzi

Previous
Chapter

of
A
A
A

CHAPTER 3

Getting started with .NET Core

Getting started with .NET Core


Now that it is clear what ASP.NET Core and .NET Core are, and why they were created, it's time to look at how to install them and how to build a simple application with them.

Installing .NET Core on Windows

Installing on Windows is pretty easy. With Visual Studio 2017, chances are you already installed it. If not, go back to the Visual Studio Installer and make sure you have the .NET Core workload selected.

Visual Studio Installer

Figure 3-1: Visual Studio Installer

Installing .NET Core on a Mac (or Linux)

The beauty of .NET Core is that it can also be installed on a Mac (or Linux for that matter) without relying on third-party frameworks, as was needed before with Mono.

Each distribution of Linux has its own individual way of installing, but in the end, the process boils down to the same principles:

  • Install some pre-requisites and configure the package manager of your distribution.
  • Invoke the package manager to download and install .NET Core and its tools.

You can read instructions specific to your distribution on the official .NET Core website. As an example, we’ll show you how to install on a Mac.

Code Listing 3-1

>brew update
>brew install openssl
>ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/
>ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/

Once all these pre-requisites have been installed, you can download and install the official SDK for macOS by downloading it from the official official .NET Core website.

On Linux and Mac, you do not have Visual Studio to develop apps, but you can use the .NET Core SDK or Visual Studio Code, which is a lightweight, extensible cross-platform text editor built by Microsoft and the community. The last chapter of this book covers in detail each of the tools with which you can build .NET Core apps.

Building Your First .NET Core Application

With so many operating systems and tools available, there are many ways to create a .NET Core application, but all the visual tools rely on the SDK to get things done. Here we are going to build our first .NET Core application using the dotnet core command line interface (CLI).

The main entry point for the SDK is the dotnet command. This command, depending on the verb used, can do lots of things, from acting as host and runner for the application to creating a new project, managing dependencies, and building applications.

Command-Line Tools

As an example, let's create a simple "Hello World" command line application using the dotnet CLI. Since the command line tools are cross-platform, the following steps can be performed on any of the supported systems: Windows, Mac, or Linux.

Open the Command Prompt (or terminal) and create an empty folder, calling it HelloWorldApp. Next, move inside this newly created folder and launch the dotnet new console command.

Now, launch the dotnet restore command to download and install all the packages and finally launch the dotnet run command to build and run the project.

Figure 3-2 shows the output of this sequence of commands.

dotnet CLI

Figure 3-2: dotnet CLI

Let's dive into what happened here.

The dotnet new console command added two files to the folder:

  • HelloWorldApp.csproj, an XML file containing the configuration of the project.
  • Program.cs, the actual code file.

The Program.cs file is pretty simple, as it just prints the Hello World! string to the console.

Code Listing 3-2

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
    }
}

What is more interesting to look at is the HelloWorldApp.csproj file. If you’ve ever looked at the project files used by Visual Studio with the full .NET Framework, you might wonder why we even discuss it. You rarely looked inside them, as they were black boxes written in XML. However, with .NET Core, they become easier to modify manually or via other commands of the dotnet tool.

Code Listing 3-3

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
 
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="**\*.cs" />
    <EmbeddedResource Include="**\*.resx" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NETCore.App">
      <Version>1.0.1</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Sdk">
      <Version>1.0.0-alpha-20161104-2</Version>
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
  </ItemGroup>
 
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Apart from the MSBuild imports, the file is basically split in three areas:

  • The first area, <PropertyGroup>, contains the properties used to configure the build—in this case, the framework it targets and the type of output.
  • The second group, <ItemGroup>, contains all the files that will be compiled or included in the build. Notice that it uses a globbing syntax (a file wildcard expansion similar to the one used in gitignore files) instead of listing every file individually, like it does with the full .NET Framework.
  • The last group, <ItemGroup>, still lists all the packages or projects that the current project depends on.

Visual Studio

Another way you can build the same console application is by using Visual Studio. First, go to the new project dialog and select Console Application (.NET Core), as shown in Figure 3-3.

New Console Application Using Visual Studio 2017

Figure 3-3: New Console Application Using Visual Studio 2017

Once the project has been created, add text to the console application and launch the project. Figure 3-4 shows Visual Studio with the console application loaded.

Hello World Application

Figure 3-4: Hello World Application

Conclusion

In this chapter, we've shown why 2016 is one of the few landmarks in our industry, together with 1996 when Microsoft released ASP Classic and 2002 when the .NET Framework and ASP.NET were released.

.NET Core is the next evolution of the Microsoft web development stack. While not as different from the .NET Framework as the .NET Framework was from ASP Classic, it still changes the foundations on which ASP.NET applications are built.

You've also seen how to install .NET Core on Windows, Mac, and Linux, and how to build a simple application using the base layer of .NET tools: the dotnet CLI. With this knowledge, you are ready to explore the new ASP.NET Core application framework.


Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.