Section 1 – Crawling
In this section, you will learn the basics of .NET Core 5, including an overview, goals/values, new features, and its history. We’ll also help you refresh your C# skills, and we’ll cover setting up your cross-platform environment, as well as building apps and pages with CSHTML, MVC, Razor Pages, and Blazor (by using a unified markup engine—Razor). Finally, we’ll explain the dependency injection software design pattern.
This section includes the following chapters:
- Chapter 1, Introduction to ASP.NET Core 5
- Chapter 2, Cross-Platform Setup
- Chapter 3, Dependency Injection
- Chapter 4, Razor View Engine
- Chapter 5, Getting Started with Blazor
Chapter 1: Introduction to ASP.NET Core 5
.NET 5 is the latest and greatest in the .NET platform. .NET 5 is the successor of .NET Core 3.1 This chapter takes a short tour through the history of the .NET Framework before diving into what this version brings to the table. The chapter wraps up with a look at utilities and tools you will want to have before proceeding with exploring the details in the chapters that follow. We will cover a broad range of topics, including cross-platform usage of .NET, different methods for creating the visual layer, backend components such as identity and data access, as well as cloud technologies.
We will cover the following topics in this chapter:
- Explaining ASP.NET Core
- Refreshing your C# knowledge
- Learning what's new with .NET 5 and C# 9
- Understanding websites and web servers
- Exploring Visual Studio Code
- Leveraging Windows Terminal
Technical requirements
This chapter includes short code snippets to demonstrate the concepts that are explained. The following software is required:
- Visual Studio 2019: Visual Studio can be downloaded from https://visualstudio.microsoft.com/vs/community/. The Community edition is free and will work for the purposes of this book.
- Visual Studio Code: Visual Studio Code can be downloaded from https://code.visualstudio.com/Download.
- .NET Core 5: The .NET Core framework can be downloaded from https://dotnet.microsoft.com/download/dotnet/5.0.
Make sure you download the SDK, and not just the runtime. You can verify the installation by opening Command Prompt and running the dotnet --info cmd as shown:
Figure 1.1 – Verifying the installation of .NET
Please visit the following link to check the CiA videos: https://bit.ly/3qDiqYY
Check out the source code for this chapter at https://github.com/PacktPublishing/ASP.NET-Core-5-for-Beginners/tree/master/Chapter%2001/Chapter_01_HelloWeb.
Explaining ASP.NET Core
The first version of .NET was released in 2002, so it doesn't sound impressive that we're getting at version 5 since it's been 18 years. However, it is slightly more complicated than that, both with the numbering system and due to various sidetracks. A complete history could possibly be a book on its own, but to understand where we are now, we will take you on a short walk down memory lane.
When .NET came on the scene, there were a couple of options available to you for choosing a programming language depending on your scenario. Visual Basic was popular for introductory type programming since it was, as the name implies, visually oriented and easy to get started with. However, VB wasn't great for writing complex applications at scale with high performance. Windows itself was mostly written in C and C++ and was the preferred route for professional-grade software. While these languages were (and still are) highly capable, they were notorious for allowing the programmer to shoot themselves in the foot due to things such as making the coder responsible for memory management and other low-level operations that were hard to debug and troubleshoot.
In parallel with the language implementations offered directly from Microsoft, Sun Microsystems released Java as a solution to these challenges. Instead of producing native code, the tooling produced managed code that abstracted memory management and made things easier. The syntax of the language was in the C++ style, so transitioning from C++ was easy for developers looking to make the switch to Java. It was also a stated goal that the code written should be portable to multiple platforms. This was enabled by a Java Virtual Machine (JVM), which was installed to execute on a given system.
Managed versus unmanaged code
Programming languages have evolved over the years. Where the first computers were programmed by physically turning switches and levers, you can now write instructions where even non-programmers are able to figure out what some of the commands mean.
One often refers to the relative closeness to the computer's native language (zeros and ones) by referring a language as low-level (close) or high-level (abstract). At the lowest level, you have languages like assembler language, which theoretically have the least overhead (provided you can find highly talented programmers), but in addition to being complex, an assembler language is not portable across different CPU architectures. C# leans more towards the other end of the spectrum, with more natural language and many of the "hard things" are hidden from the programmer. And there are also languages that are even more high-level, such as Scratch (a block-based language), targeted at kids wanting to get into programming. (There is no formal definition of low versus high.)
One of the mechanisms C# uses to achieve this is by having an intermediate layer (for .NET this is the Common Language Runtime) that translates your code in real time to the underlying machine code understood by your computer. This means that the programmer does not need to handle allocating and releasing memory, does not interfere with other program's processes, and so on, and generally does a lot of the grunt work. To cater to the developers and enable them to create applications with a minimal re-learning experience, .NET was in demand for these platforms, but .NET was not built to run without the desktop components available.
The concept is not new to or unique for C#, and it is also the concept used in Java. Originally, it was conceived back in the IBM mainframe era. On personal computers, it was initially challenging since managed code will always have an overhead due to the translation that occurs, and on resource-constrained computers (when .NET 1.0 was released), it can run slow. Newer computers handle this much more efficiently, and .NET has been optimized over the years, so for most applications, it is not much of an issue any longer if the code is managed or not.
Introducing the .NET platform
Microsoft took inspiration from Java, as well as their learnings from the ecosystem they provided, and came up with .NET. The structure of the platform is displayed in Figure 1.2.
.NET was also based on managed code and required a Common Language Runtime (CLR) to be installed to execute. The C# language was released in the same time frame, but .NET also supported Visual Basic and J#, highlighting that it was a more generic framework. Other programming languages that required extra software to be installed for running applications h...