Getting started with ASP.NET Core

Posted on by Roger Versluis.

Introduction

In this post we will setup Visual Studio and create our first ASP.NET Core application.

Getting Visual Studio

For all our backend work we will be using Visual Studio 2017 (Community Edition) and do all our coding in C# on the .NET Core framework.

Visual Studio and C# have been around for a long time and are a popular development choice in the home and enterprise world. Nowadays it’s possible to write, compile and debug C# code without Visual Studio by using editors like VS Code, Mono Develop, Rider and you are not even limited to Windows with Visual Studio for Mac. In this series we will be using Visual Studio 2017 Community Edition.

To get started download the Community edition of Visual Studio 2017 from the Visual Studio website. When starting the installer it will ask to select a Workload, here you choose what you will use Visual Studio all for. Make sure to select at least ASP.NET and web development under the Web & Cloud header. You can add more work loads or individual components but this will be enough to get started, you can always come back later and install more. Now hit continue and finish installing Visual Studio 2017.

Getting .NET Core

If you use windows you probably have heard of the .NET Framework. Currently at version 4.7.2 the .NET Framework is a standard API for software developers to program against on the Windows platform. All versions of the API are backwards compatible all the way back to version 1.0, which was released in 2000. You can imagine there are some things in there that people would like to see changed by now. In 2016 Microsoft decided it was time for a successor of the .NET Framework and the first version of .NET Core was released. This new framework is much more focussed on open source and cross-platform and is a full re-write of the original .NET framework.

.NET Core is not backwards compatible with the .NET Framework, but complies with the .NET Standard specs.

If you are more interested in the history of .NET, Richard Campbell did an excellent talk about this topic:

Before starting Visual Studio we have make sure to get the latest version of the .NET Core framework. Head over to the download section of the .NET Core website and hit Download .NET Core SDK. Complete the installer and start Visual Studio.

To make sure the SDK is installed properly you can open a command line window and execute the following command:

dotnet --info

The result will look something like:

.NET Core SDK (reflecting any global.json):
 Version:   2.2.101
 Commit:    236713b0b7

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.16299
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.2.101\

Host (useful for support):
  Version: 2.2.0
  Commit:  1249f08fed

.NET Core SDKs installed:
  2.1.502 [C:\Program Files\dotnet\sdk]
  2.2.101 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

Creating the project

In Visual Studio click File -> New -> Project. In the New Project wizard browse to .NET Core on the left toolbar, then click ASP.NET Core Web Application. In the location browse to the root of the project folder we created in the last article (i.e. C:\aspnet-angular-app). Type server in the name field and this should change the solution name at the same time. Make sure Create directory for solution is checked.

In the next screen make sure the top drop downs are set to .NET Core and ASP.NET Core 2.2. Then select the API project type. Disable Docker support and check Configure for HTTPS. Set authentication to No Authentication.

Hit OK and the project will be scaffolded.

Now click the menu item Debug -> Start Debugging or simply hit F5. The project will launch in your favorite browser and will show the output from the default generated API controller.

Exploring an ASP.NET Core Web Application

If you have ever written a web application pre ASP.NET Core you will notice some familiar files but there are some differences. A big change is that .NET Core now comes by default with its own webserver called Kestral and looks more like a traditional application with a Program.cs file with a Main. Lets explore some of the important files that are generated in an empty project.

/Program.cs
This is the main entry point of your app with the traditional Main file being executed first. WebHost.CreateDefaultBuilder will take care of settings up a full blown web application with the built in Kestral webserver.

/Startup.cs
Startup is an important part of the ASP.NET Core structure, you define what services are used throughout the application here. We will be revisiting this file periodically throughout the series.

/appsettings.json
This file hosts traditional settings like your SMTP server address or secret keys.

/Properties/launchSettings.json
When running or debugging your project the built in webserver IIS Express is started. This file contains all the settings that are applicable for IIS Express.

/Controllers/ValuesController.cs
The ValuesController is an example API controller scaffolded by the project creation wizard. It is useful for testing if your project works correctly.

Dependencies

When you right click the project name then select Manage NuGet Packages you will see a list of external dependencies for this project. At this point we can remove the Microsoft.AspNetCore.Razor.Design as we won’t be using any Razor in this project.

We are left with only 2 dependencies: Microsoft.AspNetCore.App
This contains everything you need to deploy a full blown web application.

Microsoft.NETCore.App
A set of .NET API’s that are included in the default .NET Core application model.

Conclusion

In this article we setup Visual Studio 2017 with the new .NET Core Framework. In the next steps we will switch back to Angular and setup a UI framework.