00 Intro ASP.net
00 Intro ASP.net
Choosing to learn and develop with a new framework is a big investment, so it’s
important to establish early on whether it’s right for you. In this chapter, I provide
some background on ASP.NET Core: what it is, how it works, and why you should
consider it for building your web applications.
By the end of this chapter, you should have a good overview of the benefits of
ASP.NET Core, the role of .NET 7, and the basic mechanics of how ASP.NET Core
works. So without further ado, let’s dive in!
1
2 CHAPTER 1 Getting started with ASP.NET Core
be consumed by mobile applications, and much more. ASP.NET Core runs on .NET 7,
which is the latest version of .NET Core—a high-performance, cross-platform, open-
source runtime.
ASP.NET Core provides structure, helper functions, and a framework for building
applications, which saves you from having to write a lot of this code yourself. Then the
ASP.NET Core framework code calls in to your handlers, which in turn call methods in
your application’s business logic, as shown in figure 1.1. This business logic is the core
of your application. You can interact with other services here, such as databases or
remote APIs, but your business logic typically doesn’t depend directly on ASP.NET Core.
ASP.NET Core apps can The ASP.NET framework You write these handlers Your domain can use
serve browser-based code handles the raw using primitives provided external services and
clients or can provide requests and calls in to by the framework. These databases to perform
APIs for mobile Razor Pages and web typically invoke methods its function and to
and other clients. API controller handlers. in your domain logic. persist data.
Razor Pages
Browser clients
Databases
MVC controllers Data Access
Domain
ASP.NET logic layer
Core
framework (C# (EF Core /
Minimal API
classes) Dapper)
endpoints
caches
Web API
controllers
Mobile, single-page
application, backend Remote services
servers
Client tier Presentation layer Business Logic Data Access Data tier
layer layer
Figure 1.1 A typical ASP.NET Core application consists of several layers. The ASP.NET Core framework code
handles requests from a client, dealing with the complex networking code. Then the framework calls in to
handlers (Razor Pages and Web API controllers, for example) that you write using primitives provided by the
framework. Finally, these handlers call in to your application’s domain logic—typically, C# classes and objects
without any dependencies that are specific to ASP.NET Core.
ASP.NET Core is a reimagining of the ASP.NET framework, built with modern soft-
ware design principles on top of the new .NET platform. Although it’s new in one
sense, .NET (previously called .NET Core) has had widespread production use since
2016 and has drawn significantly from the mature, stable, and reliable .NET Frame-
work, which has been used for more than two decades. You can rest easy knowing that
by choosing ASP.NET Core and .NET 7, you’re getting a dependable platform as well
as a full-featured web framework.
4 CHAPTER 1 Getting started with ASP.NET Core
One major selling point of ASP.NET Core and .NET 7 is the ability to develop and
run on any platform. Whether you’re using a Mac, Windows, or Linux computer, you
can run the same ASP.NET Core apps and develop across multiple environments. A wide
range of distributions are supported for Linux users: RHEL, Ubuntu, Debian, CentOS,
Fedora, and openSUSE, to name a few. ASP.NET Core even runs on the tiny Alpine
distribution, for truly compact deployments to containers, so you can be confident that
your operating system of choice will be a viable option.
If you’re already a .NET developer, the choice of whether to invest in ASP.NET Core
for new applications was largely a question of timing. Early versions of .NET Core lacked
some features that made it hard to adopt, but that problem no longer exists in the latest
versions of .NET. Now Microsoft explicitly advises that all new .NET applications should
use .NET 7 (or newer).
Microsoft has pledged to provide bug and security fixes for the older ASP.NET
framework, but it won’t provide any more feature updates. .NET Framework isn’t
being removed, so your old applications will continue to work, but you shouldn’t use it
for new development.
The main benefits of ASP.NET Core over the previous ASP.NET framework are
Cross-platform development and deployment
Focus on performance as a feature
A simplified hosting model
Regular releases with a shorter release cycle
Open-source
Modular features
More application paradigm options
The option to package .NET with an app when publishing for standalone
deployments
As an existing .NET developer who’s moving to ASP.NET Core, your ability to build
and deploy cross-platform opens the door to a whole new avenue of applications, such
as taking advantage of cheaper Linux virtual machine hosting in the cloud, using
Docker containers for repeatable continuous integration, or writing .NET code on
your Mac without needing to run a Windows virtual machine. ASP.NET Core, in com-
bination with .NET 7, makes all this possible.
That’s not to say that your experience deploying ASP.NET applications to Windows
and Internet Information Services (IIS) is wasted. On the contrary, ASP.NET Core
uses many of the same concepts as the previous ASP.NET framework, and you can still
run your ASP.NET Core applications in IIS, so moving to ASP.NET Core doesn’t mean
starting from scratch.
ASP.NET Core works, from a user request for a URL to the display of a page in the
browser. To get there, first you’ll see how an HTTP request works for any web server;
then you’ll see how ASP.NET Core extends the process to create dynamic web pages.
HTTP request
HTTP response
The process begins when a user navigates to a website or types a URL in their browser.
The URL or web address consists of a hostname and a path to some resource on the web
app. Navigating to the address in the browser sends a request from the user’s com-
puter to the server on which the web app is hosted, using the HTTP protocol.
The request passes through the internet, potentially to the other side of the world,
until it finally makes its way to the server associated with the given hostname, on which
the web app is running. The request is potentially received and rebroadcast at multi-
ple routers along the way, but only when it reaches the server associated with the host-
name is the request processed.
When the server receives the request, it processes that request and generates an
HTTP response. Depending on the request, this response could be a web page, an
image, a JavaScript file, a simple acknowledgment, or practically any other file. For
this example, I’ll assume that the user has reached the home page of a web app, so the
server responds with some HTML. The HTML is added to the HTTP response, which
is sent back across the internet to the browser that made the request.
As soon as the user’s browser begins receiving the HTTP response, it can start dis-
playing content on the screen, but the HTML page may also reference other pages
and links on the server. To display the complete web page instead of a static, colorless,
raw HTML file, the browser must repeat the request process, fetching every refer-
enced file. HTML, images, Cascading Style Sheets (CSS) for styling, and JavaScript
files for extra behavior are all fetched using exactly the same HTTP request process.
1.4 How does ASP.NET Core work? 7
Pretty much all interactions that take place on the internet are a facade over this
basic process. A basic web page may require only a few simple requests to render fully,
whereas a large modern web page may take hundreds. At this writing, the Amazon
.com home page (https://www.amazon.com) makes 410 requests, including requests
for 4 CSS files, 12 JavaScript files, and 299 image files!
Now that you have a feel for the process, let’s see how ASP.NET Core dynamically
generates the response on the server.
Figure 1.3 How an ASP.NET Core application processes a request. A request is received by the ASP.NET
Core application, which runs a self-hosted web server. The web server processes the request and passes it
to the body of the application, which generates a response and returns it to the web server. The web server
sends this response to the browser.
The request is received from the network by your ASP.NET Core application. Every
ASP.NET Core application has a built-in web server—Kestrel, by default—that is
responsible for receiving raw requests and constructing an internal representation of
the data, an HttpContext object, which the rest of the application can use.
8 CHAPTER 1 Getting started with ASP.NET Core
Your application can use the details stored in HttpContext to generate an appropri-
ate response to the request, which may be to generate some HTML, to return an
“access denied” message, or to send an email, all depending on your application’s
requirements.
When the application finishes processing the request, it returns the response to
the web server. The ASP.NET Core web server converts the representation to a raw
HTTP response and sends it to the network, which forwards it to the user’s browser.
To the user, this process appears to be the same as for the generic HTTP request
shown in figure 1.2: the user sent an HTTP request and received an HTTP response.
All the differences are server-side, within your application.
You’ve seen how requests and responses find their way to and from an ASP.NET
Core application, but I haven’t yet touched on how the response is generated.
Throughout this book, we’ll look at the components that make up a typical ASP.NET
Core application and how they fit together. A lot goes into generating a response in
ASP.NET Core, typically within a fraction of a second, but over the course of the book
we’ll step through an application slowly, covering each of the components in detail.
Throughout the book we’ll use a variety of examples to learn and explore concepts.
The examples are generally small and self-contained so that we can focus on a single
feature at a time.
I’ll be using Visual Studio for most of the examples in this book, but you’ll be able
to follow along using your favorite editor or integrated development environment
(IDE). Appendix A includes details on setting up your editor or IDE and installing
the .NET 7 software development kit (SDK). Even though the examples in this book
show Windows tools, everything you see can be achieved equally well on the Linux or
Mac platform.