ASP.NET Core in Action 1 Getting started with ASP.NET Core
1 Getting started with ASP.NET Core
1 ASP.NET Core 入门
This chapter covers
本章涵盖
-
What is ASP.NET Core?
什么是 ASP.NET Core? -
Things you can build with ASP.NET Core
您可以使用 ASP.NET Core 构建的内容 -
How ASP.NET Core works
ASP.NET Core 的工作原理
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.
选择使用新框架进行学习和开发是一项巨大的投资,因此尽早确定它是否适合您非常重要。在本章中,我将提供有关 ASP.NET Core的一些背景知识:它是什么、它是如何工作的,以及为什么您应该考虑使用它来构建Web应用程序。
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!
在本章结束时,您应该对ASP.NET Core的优势、.NET 7的作用以及ASP.NET Core工作原理的基本机制有一个很好的概述。因此,事不宜迟,让我们开始吧!
1.1 What is ASP.NET Core?
1.1 什么是 ASP.NET Core?
ASP.NET Core is a cross-platform, open-source application framework that you can use to build dynamic web applications quickly. You can use ASP.NET Core to build server-rendered web applications, backend server applications, HTTP APIs that can 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是一个跨平台的开源应用程序框架,可用于快速构建动态Web应用程序。您可以使用ASP.NET Core构建服务器渲染的Web应用程序、后端服务器应用程序、移动应用程序可以使用的HTTP API等等。ASP.NET Core在.NET 7上运行,这是.NET Core的最新版本,是一种高性能、跨平台、开源运行时。
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提供结构、帮助程序函数和用于构建应用程序的框架,这使您不必自己编写大量此类代码。然后,ASP.NET Core框架代码调用您的处理程序,这些处理程序反过来调用应用程序业务逻辑中的方法,如图1.1所示。此业务逻辑是应用程序的核心。您可以在此处与其他服务(例如数据库或远程 API)进行交互,但您的业务逻辑通常不直接依赖于ASP.NET Core。
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.
图 1.1 典型的 ASP.NET Core应用程序由多个层组成。ASP.NET Core框架代码处理来自客户端的请求,处理复杂的网络代码。然后,框架调用使用框架提供的基元编写的处理程序(例如 Razor Pages和Web API控制器)。最后,这些处理程序调用应用程序的域逻辑 — 通常是没有任何特定于ASP.NET Core的依赖项的C#类和对象。
1.2 What types of applications can you build?
1.2 您可以构建哪些类型的应用程序?
ASP.NET Core provides a generalized web framework that you can use to build a wide variety of applications. ASP.NET Core includes APIs that support many paradigms:
ASP.NET Core 提供了一个通用的 Web 框架,您可以使用它来构建各种应用程序。ASP.NET Core 包含支持许多范例的 API:
-
Minimal APIs—Simple HTTP APIs that can be consumed by mobile applications or browser-based single-page applications.
最小 API — 可供移动应用程序或基于浏览器的单页应用程序使用的简单 HTTP API。 -
Web APIs—An alternative approach to building HTTP APIs that adds more structure and features than minimal APIs.
Web API — 一种构建 HTTP API 的替代方法,与最小 API 相比,它增加了更多的结构和功能。 -
gRPC APIs—Used to build efficient binary APIs for server-to-server communication using the gRPC protocol.
gRPC API — 用于构建高效的二进制 API,以使用 gRPC 协议进行服务器到服务器通信。 -
Razor Pages—Used to build page-based server- rendered applications.
Razor Pages - 用于构建基于页面的服务器渲染的应用程序。 -
MVC controllers—Similar to Razor Pages. Model- View-Controller (MVC) controller applications are for server-based applications but without the page- based paradigm.
MVC 控制器 - 类似于 Razor Pages。模型-视图-控制器 (MVC) 控制器应用程序适用于基于服务器的应用程序,但没有基于页面的范例。 -
Blazor WebAssembly—A browser-based single- page application framework that uses the WebAssembly standard, similar to JavaScript frameworks such as Angular, React, and Vue.
Blazor WebAssembly - 一种基于浏览器的单页应用程序框架,它使用 WebAssembly 标准,类似于 Angular、React 和 Vue 等 JavaScript 框架。 -
Blazor Server—Used to build stateful applications, rendered on the server, that send UI events and page updates over WebSockets to provide the feel of a client-side single-page application, but with the ease of development of a server-rendered application.
Blazor Server - 用于构建在服务器上呈现的有状态应用程序,这些应用程序通过 WebSockets 发送 UI 事件和页面更新,以提供客户端单页应用程序的感觉,但易于开发服务器呈现的应用程序。
All these paradigms are based on the same building blocks of ASP.NET Core, such as the configuration and logging libraries, and then place extra functionality on top. The best paradigm for your application depends on multiple factors, including your API requirements, the details of existing applications you need to interact with, the details of your customers’ browsers and operating environment, and scalability and uptime requirements. You don’t need to choose only one of these paradigms; ASP.NET Core can combine multiple paradigms within a single application.
所有这些范例都基于ASP.NET Core的相同构建块,例如配置和日志记录库,然后将额外的功能放在顶部。应用程序的最佳范例取决于多种因素,包括 API 要求、需要与之交互的现有应用程序的详细信息、客户的浏览器和作环境的详细信息,以及可扩展性和正常运行时间要求。您不需要只选择这些范例中的一个;ASP.NET Core可以在单个应用程序中组合多个范例。
1.3 Choosing ASP.NET Core
1.3 选择 ASP.NET Core
I hope that now you have a general grasp of what ASP.NET Core is and the type of applications you can build with it. But one question remains: should you use it? Microsoft recommends that all new .NET web development use ASP.NET Core, but switching to or learning a new web stack is a big ask for any developer or company.
我希望现在您已经大致了解了 ASP.NET Core是什么以及您可以使用它构建的应用程序类型。但仍然存在一个问题:您应该使用它吗?Microsoft建议所有新的.NET Web开发都使用 ASP.NET Core,但切换到或学习新的 Web 堆栈对任何开发人员或公司来说都是一个很大的要求。
If you’re new to .NET development and are considering ASP.NET Core, welcome! Microsoft is pushing ASP.NET Core as an attractive option for web development beginners, but taking .NET cross-platform means that it’s competing with many other frameworks on their own turf. ASP.NET Core has many selling points compared with other cross-platform web frameworks:
如果你不熟悉.NET开发并正在考虑使用ASP.NET Core,欢迎使用!Microsoft正在推动 ASP.NET Core成为 Web 开发初学者的一个有吸引力的选择,但采用 .NET 跨平台意味着它正在与许多其他框架在自己的地盘上竞争。与其他跨平台Web框架相比,ASP.NET Core有很多卖点:
-
It’s a modern, high-performance, open-source web framework.
它是一个现代、高性能、开源的 Web 框架。 -
It uses familiar design patterns and paradigms.
它使用熟悉的设计模式和范例。 -
C# is a great language (but you can use VB.NET or F# if you prefer).
C# 是一种很好的语言(但如果您愿意,也可以使用 VB.NET 或 F#)。 -
You can build and run on any platform.
您可以在任何平台上构建和运行。
ASP.NET Core is a reimagining of the ASP.NET framework, built with modern software 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 Framework, 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.
ASP.NET Core是对 ASP.NET框架的重新构想,在新的 .NET 平台之上采用现代软件设计原则构建。尽管从某种意义上说,.NET(以前称为.NET Core)是新的,但它自2016年以来已被广泛用于生产领域,并且极大地借鉴了已经使用了二十多年的成熟、稳定和可靠的.NET Framework。您可以高枕无忧,因为您知道选择ASP.NET Core和.NET 7将获得一个可靠的平台以及一个功能齐全的Web框架。
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 supportedfor 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.
ASP.NET Core和.NET 7的一个主要卖点是能够在任何平台上开发和运行。无论您使用的是 Mac、Windows 还是 Linux 计算机,您都可以运行相同的 ASP.NET Core 应用程序并跨多个环境进行开发。支持多种发行版对于 Linux 用户:RHEL、Ubuntu、Debian、CentOS、Fedora 和 openSUSE等。ASP.NET Core甚至在微型Alpine 发行版上运行,以实现真正紧凑的容器部署,因此您可以确信您选择的作系统将是一个可行的选择。
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).
如果您已经是.NET 开发人员,那么选择是否为新应用程序投资 ASP.NET Core在很大程度上是一个时间问题。早期版本的.NET Core缺少一些难以采用的功能,但最新版本的.NET 中不再存在该问题。现在 Microsoft 明确建议所有新的.NET 应用程序都应该使用.NET 7(或更高版本)。
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.
Microsoft已承诺为较旧的ASP.NET框架提供bug和安全修复,但不会提供更多功能更新。.NET Framework未被删除,因此您的旧应用程序将继续工作,但您不应将其用于新的开发。
The main benefits of ASP.NET Core over the previous ASP.NET framework are
与以前的 ASP.NET 框架相比,ASP.NET Core 的主要优点是
-
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
在发布独立部署时将 .NET 与应用程序打包的选项
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 combination with .NET 7, makes all this possible.
作为正在迁移到 ASP.NET Core的现有 .NET 开发人员,您构建和部署跨平台的能力为全新的应用程序途径打开了大门,例如利用云中更便宜的 Linux 虚拟机托管,使用 Docker 容器进行可重复的持续集成,或者在 Mac 上编写 .NET 代码而无需运行 Windows 虚拟机。ASP.NET Core 与 .NET 7 相结合,使这一切成为可能。
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 应用程序部署到 Windows 和 Internet Information Services (IIS) 的经验是浪费的。相反,ASP.NET Core 使用许多与以前的 ASP.NET 框架相同的概念,并且你仍然可以在 IIS 中运行 ASP.NET Core 应用程序,因此迁移到 ASP.NET Core 并不意味着从头开始。
1.4 How does ASP.NET Core work?
1.4 ASP.NET Core 的工作原理是什么?
I’ve covered the basics of what ASP.NET Core is, what you can use it for, and why you should consider using it. In this section, you’ll see how an application built with 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.
我已经介绍了 ASP.NET Core 是什么、它可以用于什么以及为什么应该考虑使用它的基础知识。在本节中,您将了解使用 ASP.NET Core 构建的应用程序的工作原理,从用户对 URL 的请求到浏览器中的页面显示。要到达那里,首先您将了解 HTTP 请求如何适用于任何 Web 服务器;然后,您将看到 ASP.NET Core 如何扩展创建动态网页的过程。
1.4.1 How does an HTTP web request work?
1.4.1 HTTP Web 请求如何工作?
As you know now, ASP.NET Core is a framework for building web applications that serve data from a server. One of the most common scenarios for web developers is building a web app that you can view in a web browser. Figure 1.2 shows the high-level process you can expect from any web server.
正如您现在所知,ASP.NET Core 是一个框架,用于构建从服务器提供数据的 Web 应用程序。对于 Web 开发人员来说,最常见的方案之一是构建可在 Web 浏览器中查看的 Web 应用程序。图 1.2 显示了您可以从任何 Web 服务器获得的高级过程。
Figure 1.2 Requesting a web page. The user starts by requesting a web page, which causes an HTTP request to be sent to the server. The server interprets the request, generates the necessary HTML, and sends it back in an HTTP response. Then the browser can display the web page.
图 1.2 请求网页。用户首先请求一个网页,这会导致向服务器发送 HTTP 请求。服务器解释请求,生成必要的 HTML,并在 HTTP 响应中将其发送回。然后浏览器可以显示网页。
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 computer to the server on which the web app is hosted, using the HTTP protocol.
当用户导航到网站或在浏览器中键入 URL 时,该过程开始。URL 或 Web 地址由主机名和 Web 应用程序上某个资源的路径组成。导航到浏览器中的地址会使用 HTTP 协议将请求从用户的计算机发送到托管 Web 应用程序的服务器。
DEFINITION The hostname of a website uniquely identifies its location on the internet by mapping via the Domain Name Service (DNS) to an IP address. Examples include microsoft.com, www.google.co.uk, and facebook.com.
定义 网站的主机名通过域名服务 (DNS) 映射到 IP 地址,从而唯一标识其在 Internet 上的位置。示例包括 microsoft.com、www.google.co.uk 和 facebook.com。
A brief primer on HTTP
HTTP 简介
Hypertext Transfer Protocol (HTTP) is the application-level protocol that powers the web. It’s a stateless request-response protocol whereby a client machine sends a request to a server, which sends a response in turn.
超文本传输协议 (HTTP) 是支持 Web 的应用程序级协议。它是一种无状态的请求-响应协议,客户端计算机通过该协议向服务器发送请求,服务器反过来发送响应。
Every HTTP request consists of a verb indicating the type of the request and a path indicating the resource to interact with. A request typically also includes headers, which are key-value pairs, and in some cases a body, such as the contents of a form, when sending data to the server.
每个 HTTP 请求都包含一个动词(指示请求的类型)和一个路径(指示要与之交互的资源)。在向服务器发送数据时,请求通常还包括标头(键值对)和在某些情况下的正文,例如表单的内容。
An HTTP response contains a status code, indicating whether the request was successful, and optionally headers and a body.
HTTP 响应包含状态代码,指示请求是否成功,以及可选的标头和正文。
For a more detailed look at the HTTP protocol itself, as well as more examples, see section 1.3 (“A quick introduction to HTTP”) of Go Web Programming, by Sau Sheong Chang (Manning, 2016), at http://mng.bz/x4mB. You can also read the raw RFC specification at https://www.rfc-editor.org/rfc/rfc9110.txt if dense text is your thing!
有关 HTTP 协议本身的更详细内容以及更多示例,请参阅 Sau Sheong Chang (Manning, 2016) 在 http://mng.bz/x4mB 上编写的 Go Web Programming 的第 1.3 节(“HTTP 快速介绍”)。 您还可以阅读原始 RFC 规范,https://www.rfc-editor.org/rfc/rfc9110.txt 如果您喜欢密集文本!
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 multiple routers along the way, but only when it reaches the server associated with the hostname is the request processed.
该请求通过 Internet 传递,可能到达世界的另一端,直到最终到达与运行 Web 应用程序的给定主机名关联的服务器。请求可能会在途中的多个路由器上接收并重新广播,但只有当它到达与主机名关联的服务器时,才会处理请求。
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.
当服务器收到请求时,它会处理该请求并生成 HTTP 响应。根据请求,此响应可以是网页、图像、JavaScript 文件、简单确认或几乎任何其他文件。对于此示例,我假设用户具有到达 Web 应用程序的主页,因此服务器使用一些 HTML 进行响应。HTML 将添加到 HTTP 响应中,该响应将通过 Internet 发送回发出请求的浏览器。
As soon as the user’s browser begins receiving the HTTP response, it can start displaying 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 referenced 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.
一旦用户的浏览器开始接收 HTTP 响应,它就可以开始在屏幕上显示内容,但 HTML 页面也可能引用服务器上的其他页面和链接。要显示完整的网页而不是静态的、无色的原始 HTML 文件,浏览器必须重复请求过程,获取每个引用的文件。HTML、图像、用于样式的级联样式表 (CSS) 和用于额外行为的 JavaScript 文件都使用完全相同的 HTTP 请求过程来获取。
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!
几乎所有发生在互联网上的交互都是这个基本过程的假象。一个基本的网页可能只需要几个简单的请求就可以完全呈现,而一个大型的现代网页可能需要数百个请求。在撰写本文时,Amazon .com 主页 (https://www.amazon.com) 发出了 410 个请求,包括对 4 个 CSS 文件、12 个 JavaScript 文件和 299 个图像文件的请求!
Now that you have a feel for the process, let’s see how ASP.NET Core dynamically generates the response on the server.
现在您已经了解了该过程,让我们看看 ASP.NET Core 如何在服务器上动态生成响应。
1.4.2 How does ASP.NET Core process a request?
1.4.2 ASP.NET Core 如何处理请求?
When you build a web application with ASP.NET Core, browsers will still be using the same HTTP protocol as before to communicate with your application. ASP.NET Core itself encompasses everything that takes place on the server to handle a request, including verifying that the request is valid, handling login details, and generating HTML.
当您使用 ASP.NET Core 构建 Web 应用程序时,浏览器仍将使用与以前相同的 HTTP 协议与您的应用程序通信。ASP.NET Core 本身包含服务器上为处理请求而发生的所有作,包括验证请求是否有效、处理登录详细信息和生成 HTML。
As with the generic web page example, the request process starts when a user’s browser sends an HTTP request to the server, as shown in figure 1.3.
与通用网页示例一样,当用户的浏览器向服务器发送 HTTP 请求时,请求进程开始,如图 1.3 所示。
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.
图 1.3 ASP.NET Core 应用程序如何处理请求。运行自托管 Web 服务器的 ASP.NET Core 应用程序接收请求。Web 服务器处理请求并将其传递给应用程序正文,后者生成响应并将其返回给 Web 服务器。Web 服务器将此响应发送到浏览器。
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.
您的 ASP.NET Core 应用程序从网络接收请求。每个 ASP.NET Core 应用程序都有一个内置的 Web 服务器(默认为 Kestrel),该服务器负责接收原始请求并构建数据的内部表示形式,即应用程序的其余部分可以使用的 HttpContext 对象。
Your application can use the details stored in HttpContext to generate an appropriate 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.
您的应用程序可以使用存储在 HttpContext 中的详细信息来生成对请求的适当响应,这可能是生成一些 HTML、返回“拒绝访问”消息或发送电子邮件,所有这些都取决于应用程序的要求。
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.
当应用程序完成处理请求时,它会将响应返回给 Web 服务器。ASP.NET Core Web 服务器将表示形式转换为原始 HTTP 响应,并将其发送到网络,网络将其转发到用户的浏览器。
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.
对于用户来说,此过程似乎与图 1.2 中所示的通用 HTTP 请求相同:用户发送了一个 HTTP 请求并收到了一个 HTTP 响应。所有差异都是在服务器端的应用程序中。
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.
您已经了解了请求和响应如何与 ASP.NET Core 应用程序之间找到进出方式,但我尚未涉及响应是如何生成的。在本书中,我们将了解构成典型 ASP.NET Core 应用程序的组件,以及它们如何组合在一起。在 ASP.NET Core 中生成响应需要做很多工作,通常在几分之一秒内,但在本书的整个过程中我们将慢慢地逐步完成一个应用程序,详细介绍每个组件。
1.5 What you’ll learn in this book
1.5 您将在本书中学到什么
This book takes you on an in-depth tour of the ASP.NET Core framework. To benefit from the book, you should be familiar with C# or a similar object-oriented language. Basic familiarity with web concepts such as HTML and JavaScript will also be beneficial. You’ll learn the following:
本书将带您深入浏览 ASP.NET Core 框架。要从本书中受益,您应该熟悉 C# 或类似的面向对象语言。基本熟悉 HTML 和 JavaScript 等 Web 概念也将是有益的。您将了解以下内容:
-
How to build HTTP API applications using minimal APIs
如何使用最少的 API 构建 HTTP API 应用程序 -
How to create page-based applications with Razor Pages
如何使用 Razor Pages 创建基于页面的应用程序 -
Key ASP.NET Core concepts such as model-binding, validation, and routing
关键 ASP.NET 核心概念,例如模型绑定、验证和路由 -
How to generate HTML for web pages by using Razor syntax and Tag Helpers
如何使用 Razor 语法和标记帮助程序为网页生成 HTML -
How to use features such as dependency injection, configuration, and logging as your applications grow more complex
如何在应用程序变得越来越复杂时使用依赖项注入、配置和日志记录等功能 -
How to protect your application by using security best practices
如何使用安全最佳实践保护您的应用程序
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).
对于本书中的大多数示例,我将使用 Visual Studio,但您将能够使用您最喜欢的编辑器或集成开发环境 (IDE) 来学习。
Appendix A includes details on setting up your editor or IDE and installing the .NET 7 software development kit (SDK).
附录 A 包括有关设置编辑器或 IDE 以及安装 .NET 7 软件开发工具包 (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.
尽管本书中的示例展示了 Windows 工具,但您所看到的一切都可以在 Linux 或 Mac 平台上同样出色地实现。
TIP You can install .NET 7 from https://dotnet.microsoft.com/download. Appendix A contains further details on configuring your development environment to work with ASP.NET Core and .NET 7.
提示 您可以从 https://dotnet.microsoft.com/download 安装 .NET 7。附录 A 包含有关配置开发环境以使用 ASP.NET Core 和 .NET 7 的更多详细信息。
In chapter 2, we’ll look in greater depth at the types of applications you can create with ASP.NET Core. We’ll also explore its advantages over the older ASP.NET and .NET Framework platforms.
在第 2 章中,我们将更深入地了解您可以使用 ASP.NET Core 创建的应用程序类型。我们还将探讨它与旧版 ASP.NET 和 .NET Framework 平台相比的优势。
Summary
总结
-
ASP.NET Core is a cross-platform, open-source, high-performance web framework.
ASP.NET Core 是一个跨平台、开源、高性能的 Web 框架。 -
ASP.NET Core runs on .NET, previously called .NET Core.
ASP.NET Core 在 .NET(以前称为 .NET Core)上运行。 -
You can use Razor Pages or MVC controllers to build server-rendered, page-based web applications.
您可以使用 Razor Pages 或 MVC 控制器来构建服务器呈现的、基于页面的 Web 应用程序。 -
You can use minimal APIs or web APIs to build RESTful or HTTP APIs.
您可以使用最少的 API 或 Web API 来构建 RESTful 或 HTTP API。 -
You can use gRPC to build highly efficient server-to- server RPC applications.
您可以使用 gRPC 构建高效的服务器到服务器 RPC 应用程序。 -
You can use Blazor WebAssembly to build client- side applications that run in the browser and Blazor Server to build stateful, server-rendered applications that send UI updates via a WebSocket connection.
可以使用 Blazor WebAssembly 构建在浏览器中运行的客户端应用程序,使用 Blazor Server 构建有状态的服务器呈现的应用程序,这些应用程序通过 WebSocket 连接发送 UI 更新。 -
Microsoft recommends ASP.NET Core and .NET 7 or later for all new web development over the legacy ASP.NET and .NET Framework platforms.
Microsoft 建议 ASP.NET Core 和 .NET 7 或更高版本,以便在旧版 ASP.NET 和 .NET Framework 平台上进行所有新的 Web 开发。 -
Fetching a web page involves sending an HTTP request and receiving an HTTP response.
获取网页涉及发送 HTTP 请求和接收 HTTP 响应。 -
ASP.NET Core allows you to build responses to a given request dynamically.
ASP.NET Core 允许您动态构建对给定请求的响应。 -
An ASP.NET Core application contains a web server, which serves as the entry point for a request.
ASP.NET Core 应用程序包含一个 Web 服务器,该服务器用作请求的入口点。