ASP.NET Core in Action Preface

ASP.NET Core in Action Preface

preface

前言

ASP.NET has a long history; Microsoft released the first version in 2002 as part of the original .NET Framework 1.0. Since then, it's been through multiple iterations, each version bringing added features and extensibility. Each iteration, however, was built on the same underlying framework provided by System.Web.dll. This library is part of the .NET Framework, so it comes preinstalled in all versions of Windows.

ASP.NET有着悠久的历史;Microsoft于2002年发布了第一个版本,作为原始.NET Framework1.0的一部分。从那时起,它经历了多次迭代,每个版本都带来了更多的功能和可扩展性。但是,每个迭代都构建在System.Web.dll提供的相同底层框架之上。此库是.NET Framework的一部分,因此它预安装在所有版本的Windows中。

This brings mixed blessings. On one hand, the ASP.NET 4.X framework today is a reliable, battle-tested platform for building modern applications on Windows. On the other hand, it is limited by this reliance; changes to the underlying System.Web.dll are far-reaching and consequently slow to roll out, and it fundamentally excludes the many developers who are building and deploying to Linux or macOS.

这带来了喜忧参半。一方面,如今的ASP.NET4.X框架是一个可靠的、久经考验的平台,用于在Windows上构建现代应用程序。另一方面,它受到这种依赖的限制;对底层System.Web.dll的更改影响深远,因此推出速度较慢,从根本上将许多正在构建和部署到Linux或macOS的开发人员排除在外。

When I began looking into ASP.NET Core, I was one of those developers. A Windows user at heart, I was issued a Mac by my employer, so I was stuck working in a virtual machine all day. ASP.NET Core promised to change all that, allowing me to develop natively on both my Windows machine and my Mac.

当我开始研究ASP.NET Core时,我就是其中之一。我本质上是Windows用户,我的雇主给我发了一台Mac,所以我整天都被困在虚拟机中工作。ASP.NET Core承诺改变这一切,让我可以在Windows机器和Mac上进行原生开发。

I was relatively late to the party in many respects, taking an active interest only just before the RC2 release of ASP.NET Core. By this point there had already been eight (!) beta releases, many of which contained significant breaking changes. By not diving in fully until RC2, I was spared the pain of dodgy tooling and changing APIs.

在许多方面,我加入派对的时间相对较晚,只是在ASP.NET Core的RC2发布之前才开始积极关注。此时已经有8个beta版本,其中许多都包含重大的破坏性变化。由于直到RC2才完全深入研究,我免于狡猾的工具和更改API的痛苦。

What I saw at that point really impressed me. ASP.NET Core let developers use their existing knowledge of the .NET Framework, and of ASP.NET MVC applications in particular, while baking in current best practices such as dependency injection, strongly typed configuration, and logging. On top of that, you could build and deploy cross-platform. I was sold.

我当时所看到的真的给我留下了深刻的印象。ASP.NET Core允许开发人员使用他们现有的.NET Framework知识,特别是ASP.NET MVC应用程序,同时采用当前的最佳实践,如依赖项注入、强类型配置和日志记录。最重要的是,您可以跨平台构建和部署。我被说服了。

This book came about largely due to my approach to learning about ASP.NET Core. Rather than simply reading documentation and blog posts, I decided to try something new and start writing about what I learned. Each week I would dedicate some time to exploring a new aspect of ASP.NET Core, and I’d write a blog post about it. When the possibility of writing a book came about, I jumped at the chance—another excuse to dive further into the framework!

这本书的诞生很大程度上是由于我学习ASP.NET Core的方法。我决定尝试一些新的东西,并开始写下我学到的东西,而不是简单地阅读文档和博客文章。每周我都会花一些时间来探索ASP.NET Core的一个新方面,并写一篇关于它的博客文章。当写书的可能性出现时,我抓住了这个机会——这是进一步深入研究框架的另一个借口!

Since I started this book, a lot has changed, both with the book and ASP.NET Core. The first major release of the framework in June 2016 still had many rough edges, in particular around the tooling experience. With the release of
.NET 7 in November 2022, ASP.NET Core has really come into its own, with the APIs and tooling reaching mature levels.

自从我开始写这本书以来,这本书和ASP.NETCore都发生了很多变化。该框架的第一个主要版本于2016年6月发布,但仍有许多粗糙的边缘,尤其是在工具体验方面。随着.NET7在2022年11月发布,ASP.NET Core已经真正崭露头角,API和工具达到了成熟的水平。

Updates to the framework in .NET 6 and .NET 7 significantly simplified the getting-started experience for newcomers with the introduction of minimal hosting and minimal APIs, which provide a terser, simpler approach to writing APIs, much closer to the experience in other languages. You can get straight into building your app's functionality without having to understand architecture first.

.NET6和.NET7中对框架的更新通过引入最小托管和最小API显著简化了新手的入门体验,这些API提供了一种更简洁、更简单的API编写方法,更接近其他语言中的体验。您可以直接开始构建应用程序的功能,而无需先了解架构。

For some experienced ASP.NET Core developers, these changes can feel regressive and unstructured, but if you're one of them, I encourage you to give them a chance and to build your own structure and patterns. For brevity and clarity of the examples in this book, I often put the whole code for your app in one file, but don't think that's how you need to write your real applications. You're free to create helper methods, classes, and any structure that helps keep your applications maintainable while taking advantage of the performance benefits of minimal APIs.

对于一些经验丰富的ASP.NET Core开发人员来说,这些变化可能会让人感觉是倒退和非结构化的,但如果你是其中之一,我鼓励你给他们一个机会,并构建你自己的结构和模式。为了简洁明了地介绍本书中的示例,我经常将应用程序的整个代码放在一个文件中,但不要认为这是编写实际应用程序的方式。您可以自由创建帮助程序方法、类和任何结构,以帮助保持应用程序的可维护性,同时利用最小API的性能优势。

This book covers everything you need to get started with ASP.NET Core, whether you’re new to web development or an existing ASP.NET developer. It focuses on the framework itself, so I don’t go into details about client-side frameworks such as Angular and React or technologies like Docker. I also don’t cover all the new features in .NET 7, such as Blazor and gRPC; instead, I provide links where you can find more information.

本书涵盖了开始使用ASP.NETCore所需的一切,无论您是Web开发新手还是现有的ASP.NET开发人员。它侧重于框架本身,因此我不会详细介绍客户端框架(如Angular和React)或Docker等技术。我也没有介绍.NET7中的所有新功能,例如Blazor和gRPC;相反,我提供了链接,您可以在其中找到更多信息。

In this edition, I have significantly expanded and rearranged many chapters compared with previous editions of the book; some chapters have been split into more manageable sizes.

与本书的前几个版本相比,在这个版本中,我显着扩展和重新排列了许多章节;一些章节被分成了更易于管理的大小。

The early chapters feature a lot of new content focusing on minimal APIs and minimal hosting introduced in .NET 6.

前几章包含许多新内容,重点介绍.NET6中引入的最小API和最小托管。

I find it a joy to work with ASP.NET Core apps compared with apps using the previous version of ASP.NET, and I hope that my passion comes through in this book!

与使用以前版本的ASP.NET的应用程序相比,我发现使用ASP.NETCore应用程序是一种乐趣,我希望我的热情在这本书中得到体现!

acknowledgments‌

致谢

Although there is only one name on the cover of this book, a plethora of people contributed to both its writing and production. In this section I’d like to thank everyone who encouraged me, contributed, and put up with me for the past year.

虽然这本书的封面上只有一个名字,但很多人都为它的写作和制作做出了贡献。在本节中,我要感谢过去一年中所有鼓励我、做出贡献和容忍我的人。

First, and most important, I’d like to thank my girlfriend, Becky. Your continual support and encouragement means the world to me and has kept me going through such a busy time. You’ve taken the brunt of my stress and pressure, and I’m eternally grateful. I love you always.

首先,也是最重要的,我要感谢我的女朋友Becky。您一直以来的支持和鼓励对我来说意味着整个世界,让我度过了如此忙碌的时光。你首当其冲地承受了我的压力和压力,我永远感激不尽。我永远爱你。

I’d also like to thank my whole family for their support, in particular my parents, Jan and Bob, for putting up with my ranting; my sister, Amanda, for your always upbeat chats; and of course, Goose, for diligently ensuring that I take regular breaks for walks and tummy tickles.

我还要感谢我全家人的支持,特别是我的父母Jan和Bob,他们忍受了我的咆哮;我的姐姐Amanda,感谢你总是乐观的聊天;当然,还有Goose,他勤奋地确保我定期休息散步和挠肚子痒痒。

On a professional level, I’d like to thank Manning for giving me this opportunity. Brian Sawyer “discovered” me for the first version of this book and encouraged me to tackle the subsequent versions. Marina Michaels served as my development editor for the third time running and again proved to be alternately meticulous, critical, encouraging, and enthusiastic. The book is undoubtedly better thanks to your involvement.

在专业层面上,我要感谢Manning给我这个机会。BrianSawyer在这本书的第一个版本中“发现”了我,并鼓励我处理后续版本。MarinaMichaels担任我的第三次担任开发编辑,并再次证明他时而细致、批判、鼓励和热情。多亏了您的参与,这本书无疑会更好。

Thank you to my review editor, Adriana Sabo, and to all the reviewers: Alen Adanić, Ben McNamara, Bela Istók, Darrin Bishop, Dennis Liabenow, Al Pezewski, Emmanouil Chardalas, Foster Haines, Onofrei George, John Guthrie, Jean-François Morin, Pedro Seromenho, Joe Cuevas, José Antonio Martinez Perez, Joe Suchy, Luis Moux, Milan Šarenac, Milorad Imbra, Nik Rimington, Nitin Ainani, Oliver Korten, Raushan Jha, Richard Young, Rick Beerendonk, Ron Lease, Ruben Vandeginste, Sumit K. Singh, Towhidul Bashar, Daniel Vásquez, and Will Lopez. Your suggestions helped make this a better book.

感谢我的评论编辑AdrianaSabo和所有评论者:AlenAdanić、BenMcNamara、BelaIstók、DarrinBishop、DennisLiabenow、AlPezewski、EmmanouilChardalas、FosterHaines、OnofreiGeorge、JohnGuthrie、Jean-FrançoisMorin、PedroSeromenho、JoeCuevas、JoséAntonioMartinezPerez、JoeSuchy、LuisMoux、MilanŠarenac、MiloradImbra、NikRimington、NitinAinani、OliverKorten、RaushanJha、RichardYoung、RickBeerendonk、RonLease、RubenVandeginste、SumitK.Singh、TowhidulBashar、DanielVásquez和WillLopez。您的建议帮助使这本书变得更好。

My thanks go to the technical editor for this book, Filip Wojcieszyn, who is a founder and maintainer of several popular open-source projects, frequent conference speaker, and a Microsoft MVP. Filip provided invaluable feedback, highlighting my incorrect assumptions and technical biases, and ensuring technical correctness in everything I wrote.

我要感谢本书的技术编辑Filip Wojcieszyn,他是几个流行的开源项目的创始人和维护者,经常在会议上发表演讲,并且是Microsoft MVP。Filip提供了宝贵的反馈,强调了我的错误假设和技术偏见,并确保我写的所有内容的技术正确性。

I also wish to thank Tanya Wilke, who served as technical proofreader. Tanya verified that the code I wrote actually ran and made sense, working through the chapters with formidable efficiency.

我还要感谢担任技术校对员的Tanya Wilke。Tanya验证了我编写的代码确实运行良好且有意义,以惊人的效率完成了各个章节。

To everyone at Manning who helped get this book published and marketed, a heartfelt thanks. I’d also like to thank all the MEAP readers for their comments, which helped improve the book in numerous ways.

衷心感谢Manning帮助出版和营销这本书的每个人。我还要感谢大家MEAP读者的评论,这在许多方面帮助改进了这本书。

I would have never been in a position to write this book if not for the excellent content produced by members of the .NET community and those I follow on social media.

如果不是.NET社区和我在社交媒体上关注的人。

Finally, thanks to all those friends who encouraged and supported me, and showed interest generally. We may not have been able to meet up as much as we’d like, but I look forward to getting together for a drink as soon as it’s possible.

最后,感谢所有鼓励和支持我的朋友,并普遍表现出兴趣。我们可能无法如愿以偿地见面,但我期待着尽快聚在一起喝一杯。

about this book‌

关于本书

This book is about the ASP.NET Core framework, what it is, and how you can use it to build web applications. Although some of this content is already available online, it’s scattered around the internet in disparate documents and blog posts. This book guides you through building your first applications, introducing additional complexity as you cement previous concepts.

这本书介绍了ASP.NET Core框架、它是什么以及如何使用它来构建Web应用程序。尽管其中一些内容已经在网上提供,但它们分散在互联网上不同的文档和博客文章中。本书将指导您构建您的第一个应用程序,并在巩固以前的概念时引入额外的复杂性。

I present each topic using relatively small examples rather than building on a single example application through the book. There are merits to both approaches, but I wanted to ensure that the focus remained on the specific topics being taught, without the mental overhead of navigating an increasingly large project.

我使用相对较小的示例来介绍每个主题,而不是在本书中构建单个示例应用程序。这两种方法都有优点,但我想确保重点仍然放在所教授的特定主题上,而不会因驾驭越来越大的项目而产生脑力开销。

By the end of the book, you should have a solid understanding of how to build apps with ASP.NET Core, its strengths and weaknesses, and how to use its features to build apps securely. I don’t spend a lot of time on application architecture, but I make sure to point out best practices, especially where I cover architecture only superficially for the sake of brevity.

在本书结束时,您应该对如何使用ASP.NETCore构建应用程序、其优缺点以及如何使用其功能安全地构建应用程序有深入的了解。我不会在应用程序架构上花费太多时间,但我会确保指出最佳实践,尤其是为了简洁起见,我只是肤浅地介绍架构。

Who should read this book

谁应该阅读这本书

This book is for C# developers who are interested in learning a cross-platform web framework. It doesn’t assume that you have any experience building web applications. You may be a mobile or desktop developer, for example, though experience with ASP.NET or another web framework is undoubtedly beneficial.

本书适用于对学习跨平台Web框架感兴趣的C#开发人员。它不假定您具有构建Web应用程序的任何经验。例如,您可能是移动或桌面开发人员,但具有ASP.NET或其他Web框架的经验无疑是有益的。

I assume that in addition to a working knowledge of C# and .NET, you have some knowledge of common object-oriented practices and a basic understanding of relational databases in general. I assume passing familiarity with HTML and CSS and of JavaScript’s place as a client-side scripting language. You don’t need to know any JavaScript or CSS frameworks for this book, though ASP.NET Core works well with both if that is your forte.

我假设除了C#和.NET中,您对常见的面向对象的做法有一定的了解,并且对关系数据库有基本的了解。我假设你对HTML和CSS以及JavaScript作为客户端脚本语言的地位有所了解。对于本书,您不需要了解任何JavaScript或CSS框架,但如果这是您的强项,ASP.NET Core可以很好地与两者配合使用。

Web frameworks naturally touch on a wide range of topics, from the database and network to visual design and client- side scripting. I provide as much context as possible, and I include links to sites and books where you can learn more.

Web框架自然涉及广泛的主题,从数据库和网络到可视化设计和客户端脚本。我提供了尽可能多的背景信息,并提供了指向您可以了解更多信息的网站和书籍的链接。

How this book is organized

本书的组织方式

This book is divided into 5 parts, 36 chapters, and 2 appendices. Ideally, you will read the book cover to cover and then use it as a reference, but I realize that this approach won’t suit everyone. Although I use small sample apps to demonstrate a topic, some chapters build on the work of previous ones, so the content will make more sense when read sequentially.

本书分为5个部分、36章和2个附录。理想情况下,你会从头到尾阅读这本书,然后将其用作参考,但我意识到这种方法并不适合所有人。虽然我使用小型示例应用程序来演示主题,但有些章节建立在前几章的基础上,因此按顺序阅读时,内容会更有意义。

I strongly suggest reading the chapters in part 1 in sequence, as each chapter builds on topics introduced in the previous chapters and provides a basis for the rest of the book. Part 2 is also best read sequentially, though most of the chapters are independent if you wish to jump around.

我强烈建议按顺序阅读第1部分中的章节,因为每一章都建立在前几章介绍的主题之上,并为本书的其余部分提供了基础。第2部分也最好按顺序阅读,但如果您想跳来跳去,大多数章节都是独立的。

Part 3, again, is best read sequentially. You’ll get the best experience by reading the chapters in parts 4 and 5 sequentially, but many of the topics are independent, so you can read them out of order if you prefer. But I recommend only doing so after you’ve covered parts 1 to 3.

同样,第3部分最好按顺序阅读。按顺序阅读第4部分和第5部分中的章节将获得最佳体验,但许多主题是独立的,因此如果您愿意,可以不按顺序阅读它们。但我建议仅在您完成第1部分到第3部分后才这样做。

Part 1 provides a general introduction to ASP.NET Core, focusing on building small JSON APIs by using the latest features introduced in .NET 7. After we cover the basics, we look at building minimal API applications that provide the simplest programming model for ASP.NET Core web applications.

第1部分提供了ASP.NET Core的一般介绍,重点介绍如何使用.NET7中引入的最新功能构建小型JSONAPI。在介绍基础知识之后,我们将了解如何构建最小的API应用程序,这些应用程序为ASP.NETCoreWeb应用程序提供最简单的编程模型。

  • Chapter 1 introduces ASP.NET Core and its place in the web development landscape. It describes the type of applications you can build, some of the reasons to choose ASP.NET Core, and the basics of web requests in an ASP.NET Core application.

  • 第1章介绍了ASP.NETCore及其在Web开发领域中的地位。它介绍了您可以构建的应用程序类型、选择ASP.NETCore的一些原因,以及ASP.NET Core应用程序中Web请求的基础知识。

  • Chapter 2 looks at why you should consider using any web framework, why ASP.NET Core was created, and the different application paradigms you can use with ASP.NET Core. Finally, it looks at the situations when you should and shouldn’t choose ASP.NET Core.

  • 第2章探讨了为什么应该考虑使用任何Web框架,创建ASP.NETCore的原因,以及可以与ASP.NET Core一起使用的不同应用程序范例。最后,它着眼于您应该和不应该选择ASP.NET Core的情况。

  • Chapter 3 walks through all the components of a basic ASP.NET Core minimal API application, discussing their role and how they combine to generate a response to a web request.

  • 第3章介绍了基本ASP.NET Core最小API应用程序的所有组件,讨论了它们的作用以及它们如何组合以生成对Web请求的响应。

  • Chapter 4 describes the middleware pipeline, the main application pipeline in ASP.NET Core, which defines how incoming requests are processed and how a response should be generated.

  • 第4章介绍了中间件管道,这是ASP.NET Core中的主要应用程序管道,它定义了如何处理传入请求以及如何生成响应。

  • Chapter 5 shows how to use minimal API endpoints to create a JavaScript Object Notation (JSON) HTTP API that can be called by client-side apps, server-side apps, or mobile devices.

  • 第5章展示了如何使用最少的API端点来创建可由客户端应用程序、服务器端应用程序或移动设备调用的JavaScript对象表示法(JSON)HTTP API

  • Chapter 6 describes the ASP.NET Core routing system. Routing is the process of mapping incoming request URLs to a specific handler method, which executes to generate a response.

  • 第6章介绍了ASP.NET Core路由系统。路由是将传入请求URL映射到特定处理程序方法的过程,该方法执行以生成响应。

  • Chapter 7 looks at model binding in minimal APIs, the process of mapping form data and URL parameters passed in a request to concrete C# objects.

  • 第7章着眼于最小API中的模型绑定,以及将请求中传递的表单数据和URL参数映射到具体C#对象的过程。

Part 2 covers important topics for building fully-featured web applications after you understand the basics:

在了解了基础知识之后,第2部分将介绍构建功能齐全的Web应用程序的重要主题:

  • Chapter 8 introduces the concept of dependency injection (DI) and describes the DI container built into ASP.NET Core.

  • 第8章介绍了依赖关系注入(DI)的概念,并介绍了ASP.NETCore中内置的DI容器。

  • Chapter 9 builds on chapter 8 by describing how to register your own services with the DI container, the patterns you can use, and how to understand the lifetime of services the DI container creates.

  • 第9章以第8章为基础,介绍了如何向DI容器注册您自己的服务、您可以使用的模式以及如何了解DI容器创建的服务的生命周期。

  • Chapter 10 discusses how to read settings and secrets in ASP.NET Core, and how to map them to strongly typed objects.

  • 第10章讨论了如何读取ASP.NET Core中的设置和密钥,以及如何将它们映射到强类型对象。

  • Chapter 11 describes how to document your APIs using the OpenAPI standard and how this helps with testing scenarios and for automatically generating clients to call your APIs.

  • 第11章描述了如何使用OpenAPI标准记录您的API,以及它如何帮助测试场景和自动生成客户端来调用API。

  • Chapter 12 introduces EntityFramework Core (EF Core) for saving data in a relational database.

  • 第12章介绍了用于在关系数据库中保存数据的EntityFramework Core(EFCore)。

Part 3 moves away from minimal APIs and looks at how to build server-rendered page-based HTML applications using Razor Pages and the Model-View-Controller (MVC) architecture:

第3部分从最少的API出发,介绍如何使用RazorPages和模型-视图-控制器(MVC)体系结构构建服务器呈现的基于页面的HTML应用程序:

  • Chapter 13 shows how to use Razor Pages to build page-based web sites. Razor Pages are the recommended way to build server-rendered applications in ASP.NET Core and are designed for page-based applications.

  • 第13章介绍如何使用RazorPages构建基于页面的网站。RazorPages是在ASP.NET Core中构建服务器呈现的应用程序的推荐方法,专为基于页面的应用程序而设计。

  • Chapter 14 describes the Razor Pages routing system and how it differs from minimal APIs.

  • 第14章介绍了RazorPages路由系统以及它与最小API的区别。

  • Chapter 15 looks at page handlers in Razor Pages, which are responsible for choosing how to respond to a request and selecting what response to generate.

  • 第15章介绍了RazorPages中的页面处理程序,这些处理程序负责选择如何响应请求并选择要生成的响应。

  • Chapter 16 looks at model binding in Razor Pages, how it differs from minimal APIs, and the importance of validating your models.

  • 第16章介绍了RazorPages中的模型绑定、它与最小API的区别,以及验证模型的重要性。

  • Chapter 17 shows how to generate HTML web pages using the Razor template language.

  • 第17章介绍如何使用Razor模板语言生成HTML网页。

  • Chapter 18 builds on chapter 17 by introducing Tag Helpers, which can greatly reduce the amount of code required to build forms and web pages.

  • 第18章在第17章的基础上引入了标记帮助程序,这可以大大减少构建表单和网页所需的代码量。

  • Chapter 19 introduces MVC controllers as an alternative approach to building both server- rendered HTML applications and API applications.

  • 第19章介绍了MVC控制器作为构建服务器渲染的HTML应用程序和API应用程序的替代方法。

  • Chapter 20 describes how to use MVC controllers to build APIs that can be called by client-side apps as an alternative to minimal APIs.

  • 第20章描述了如何使用MVC控制器构建可由客户端应用程序调用的API,作为最小API的替代方案。

  • Chapter 21 introduces the MVC and Razor Pages filter pipeline, shows how it works, and describes some of the filters built into the framework.

  • 第21章介绍了MVC和RazorPages筛选器管道,展示了它的工作原理,并介绍了框架中内置的一些筛选器。

  • Chapter 22 builds on chapter 21 by showing how to create custom filters to reduce some of the duplication in your MVC and Razor Pages applications.

  • 第22章以第21章为基础,展示了如何创建自定义筛选器以减少MVC和RazorPages应用程序中的一些重复。

The chapters that make up part 4 cover important cross- cutting aspects of ASP.NET Core development:

构成第4部分的章节涵盖了ASP.NETCore开发的重要跨领域方面:

  • Chapter 23 describes how to add user profiles and authentication to your application by using ASP.NET Core Identity.

  • 第23章描述了如何使用ASP.NETCoreIdentity向应用程序添加用户配置文件和身份验证。

  • Chapter 24 builds on the previous chapter by introducing authorization for users so you can restrict which pages a signed-in user can access.

  • 第24章在上一章的基础上引入了用户授权,以便您可以限制已登录用户可以访问的页面。

  • Chapter 25 discusses authentication and authorization for API applications, how this differs from authentication in HTML applications, and how to get started with authentication in ASP.NET Core APIs.

  • 第25章讨论了API应用程序的身份验证和授权,这与HTML应用程序中的身份验证有何不同,以及如何开始在ASP.NET Core API中进行身份验证。

  • Chapter 26 shows how to configure logging in your application and how to write log messages to multiple locations.

  • 第26章展示了如何在应用程序中配置日志记录以及如何将日志消息写入多个位置。

  • Chapter 27 looks at how to publish your app and configure it for a production environment.

  • 第27章介绍了如何发布应用程序并针对生产环境对其进行配置。

  • Chapter 28 discusses the reason for adding HTTPS to your application, how to use HTTPS when developing locally and in production, and how to force HTTPS for your whole application.

  • 第28章讨论了将HTTPS添加到应用程序的原因,在本地和生产环境中开发时如何使用HTTPS,以及如何为整个应用程序强制使用HTTPS。

  • Chapter 29 explores some other security considerations you should make when developing your application and how to stay safe with ASP.NET Core.

  • 第29章探讨了在开发应用程序时应考虑的其他一些安全注意事项,以及如何使用ASP.NETCore保持安全。

Part 5 looks at various topics that help you take your ASP.NET Core applications further, including nonweb applications, custom configuration and components, and testing:

第5部分将介绍各种主题,这些主题可帮助您进一步改进ASP.NETCore应用程序,包括非Web应用程序、自定义配置和组件以及测试:

  • Chapter 30 discusses an alternative bootstrapping approach for ASP.NET Core apps, using the generic host and a Startup class.

  • 第30章讨论了ASP.NETCore应用程序的另一种引导方法,使用通用host和Startup类。

  • Chapter 31 describes how to build and use a variety of custom components, such as custom middleware, and how to handle complex configuration requirements.

  • 第31章描述了如何构建和使用各种自定义组件,例如自定义中间件,以及如何处理复杂的配置要求。

  • Chapter 32 expands on chapter 31, showing how to build custom Razor Page components such as custom Tag Helpers and custom validation attributes.

  • 第32章对第31章进行了扩展,展示了如何构建自定义Razor页面组件,例如自定义标记帮助程序和自定义验证属性。

  • Chapter 33 discusses the IHttpClientFactory service and how to use it to create HttpClient instances for calling remote APIs.

  • 第33章讨论了IHttpClientFactory服务以及如何使用它来创建用于调用远程API的HttpClient实例。

  • Chapter 34 explores the generic IHost abstraction, which you can use to create Windows Services and Linux daemons. You’ll also learn to run tasks in the background of your applications.

  • 第34章探讨了通用的IHost抽象,您可以使用它来创建Windows服务和Linux守护程序。您还将学习如何在应用程序的后台运行任务。

  • Chapter 35 shows how to test an ASP.NET Core application with the xUnit testing framework.

  • 第35章展示了如何使用xUnit测试框架测试ASP.NET Core应用程序。

  • Chapter 36 follows on from chapter 35, showing how to test ASP.NET Core applications specifically. It covers both unit tests and integration tests using the Test Host.

  • 第36章是第35章的后续内容,专门展示了如何测试ASP.NET Core应用程序。它涵盖单元测试和使用TestHost的集成测试。

The two appendices provide supplementary information:

两个附录提供了补充信息:

  • Appendix A describes how to configure your development environment, whether you’re in Windows, Linux, or macOS.

  • 附录A介绍了如何配置开发环境,无论您使用的是Windows、Linux还是macOS。

  • Appendix B contains links that I’ve found useful in learning about ASP.NET Core.

  • 附录B包含我发现对了解ASP.NETCore有用的链接。

About the code

关于代码

Source code is provided for all chapters except chapters 1, 2, 21, and 27, which don’t have any code. You can view the source code for each chapter in my GitHub repository at https://github.com/andrewlock/asp-dot-net-core-in-action-3e. A zip file containing all the source code is also available on the publisher’s website at https://www.manning.com/books/asp-net-core-in-action-third-edition. You can get executable snippets of code from the liveBook (online) version of this book at https://livebook.manning.com/book/asp-net-core-in-action-third-edition.

除了第1、2、21和27章之外,所有章节都提供了源代码,它们没有任何代码。您可以在https://github.com/andrewlock/asp-dot-net-core-in-action-3e 的我的GitHub存储库中查看每章的源代码。出版商的网站上也提供了包含所有源代码的zip文件,网址为https://livebook.manning.com/book/asp-net-core-in-action-third-edition。您可以从本书的liveBook(在线)版本(https://livebook.manning.com/book/asp-net-core-in-action-third-edition )获取可执行的代码片段。

All the code examples in this book use .NET 7 and were built using both Visual Studio and Visual Studio Code. To build and run the examples, you need to install the .NET software development kit (SDK), as described in appendix A.

本书中的所有代码示例都使用.NET7,并且是使用VisualStudio和VisualStudio Code构建的。构建并运行示例,您需要安装.NET软件开发工具包(SDK),如附录A中所述。

This book contains many examples of source code, both in numbered listings and inline with normal text. In both cases, source code is formatted in a fixed-width font like this to separate it from ordinary text. Sometimes code is also in bold to highlight changes from previous steps in the chapter, such as when a new feature adds to an existing line of code.

本书包含许多源代码示例,包括编号列表和与普通文本内联的源代码。在这两种情况下,源代码都采用固定宽度字体的格式,以便将其与普通文本区分开来。有时,代码也以粗体显示,以突出显示与本章中前面步骤相比的更改,例如,当新功能添加到现有代码行时。

In many cases, the original source code has been reformatted; we’ve added line breaks and reworked indentation to accommodate the available page space in the book. In rare cases, even this was not enough, and some listings include line-continuation markers (➥).

在许多情况下,原始源代码已被重新格式化;我们添加了换行符并重新设计了缩进,以适应书籍中可用的页面空间。在极少数情况下,即使这样还不够,一些商品信息包含行继续标记(➥)。

Additionally, comments in the source code have been removed from the listings when the code is described in the text. Code annotations accompany many of the listings, highlighting important concepts.

此外,当文本中描述代码时,源代码中的注释已从列表中删除。许多清单都附有代码注释,突出了重要的概念。

liveBook discussion forum

liveBook论坛

Purchase of ASP.NET Core in Action, Third Edition, includes free access to liveBook, Manning’s online reading platform. Using liveBook’s exclusive discussion features, you can attach comments to the book globally or to specific sections or paragraphs. It’s a snap to make notes for yourself, ask and answer technical questions, and receive help from the author and other users. To access the forum, go to https://livebook.manning.com/book/asp-net-core-in-action-third-edition/discussion. You can also learn more about Manning’s forums and the rules of conduct at https://livebook.manning.com/discussion.

购买ASP.NET Corein Action,ThirdEdition,即可免费访问Manning的在线阅读平台liveBook。使用liveBook的独有讨论功能,您可以将评论全局附加到书籍或特定部分或段落。您可以为自己做笔记,提出和回答技术问题,并从作者和其他用户那里获得帮助。要访问论坛,请转到https://livebook.manning.com/book/asp-net-core-in-action-third-edition/discussion。您还可以在https://livebook.manning.com/discussion上了解有关Manning论坛和行为准则的更多信息。

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We suggest that you try asking the author some challenging questions lest his interest stray!

曼宁对读者的承诺是提供一个场所,让读者个人之间以及读者与作者之间可以进行有意义的对话。这并不是作者对任何特定参与量的承诺,他对论坛的贡献仍然是自愿的(并且是无偿的)。我们建议您尝试向作者询问一些具有挑战性的问题,以免他的兴趣偏离!

The forum and the archives of previous discussions will be accessible on the publisher’s website as long as the book is in print.

只要这本书是印刷的,就可以在出版商的网站上访问论坛和以前讨论的档案。

about the author‌

关于作者

alt text

ANDREW LOCK is a .NET developer and Microsoft MVP. He graduated with an engineering degree from Cambridge University, specializing in software engineering, and went on to obtain a PhD in digital image processing. He has been developing professionally with .NET since 2010, using a wide range of technologies, including WinForms, ASP.NET WebForms, ASP.NET MVC, ASP.NET Webpages, and most recently ASP.NET Core. Andrew has put many ASP.NET Core applications into production since version 1 was released in 2016. He has an active blog at https://andrewlock.net dedicated to ASP.NET Core. This blog has frequently been featured in the community spotlight by the ASP.NET team at Microsoft, on the .NET blog, and in the weekly community standups.

ANDREW LOCK是一名.NET开发人员和MicrosoftM VP。他毕业于剑桥大学,获得工程学位,专攻软件工程,并继续获得数字图像处理博士学位。自2010年以来,他一直在使用.NET进行专业开发,使用各种技术,包括WinForms、ASP.NET WebForms、ASP.NET MVC、ASP.NET Webpages,以及最近的ASP.NET Core。自2016年发布第1版以来,Andrew已经将许多ASP.NET Core应用程序投入生产。他在 https://andrewlock.net 上有一个活跃的博客,专门用于ASP.NET Core。此博客经常出现在Microsoft的ASP.NET团队的社区聚光灯、.NET博客和每周社区站立会议中。

about the cover illustration‌

关于封面插图

alt text

The caption for the illustration on the cover of ASP.NET Core in Action, Third Edition, is “The Captain Pasha. Kapudan pasha, admiral of the Turkish navy,” taken from a collection published in 1802 by William Miller.

《ASP.NET Corein Action》第三版封面上的插图说明是“帕夏船长。Kapudanpasha,土耳其海军上将”,摘自威廉·米勒(William Miller)于1802年出版的文集。

In those days, it was easy to identify where people lived and what their trade or station in life was by their dress alone.

在那些日子里,仅凭他们的衣着很容易确定人们居住的地方以及他们的职业或生活地位。

Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional culture centuries ago, brought back to life by pictures from collections such as this one.

Manning以几个世纪前丰富多样的地区文化为基础的书籍封面来庆祝计算机行业的创造力和主动性,这些书籍通过像这样的收藏中的图片重新焕发了生机。

Leave a Reply

Your email address will not be published. Required fields are marked *