28,99 €
Build cutting-edge web applications using ASP.NET Core 9 with this comprehensive guide from Microsoft MVP Albert Tanure, a seasoned .NET expert with over 20 years of experience as a cloud solutions architect. Albert brings unparalleled expertise in building modern web applications to ASP.NET Core 9.0 Essentials to help you kickstart your journey on the right foot.
Starting with the platform’s fundamental concepts, you’ll work through practical exercises to develop web applications with powerful UI frameworks, deliver flexible and scalable solutions through an API approach, and explore advanced topics such as customizing request flows and implementing robust security measures. You’ll get to grips with cloud-native practices to prepare your applications for cloud environments. Whether you’re a beginner or an experienced developer, this book is your guide to building modern, secure, and adaptable web applications with ASP.NET Core.
By the end of this book, you’ll be proficient in leveraging the advanced features of ASP.NET Core 9 to create cloud-ready web applications for diverse market segments.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 456
Veröffentlichungsjahr: 2025
ASP.NET Core 9.0 Essentials
Explore the .NET Core web stack, including Razor Pages, Blazor, and MVC, to build cloud-ready apps
Albert S. Tanure
Copyright © 2025 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Group Product Manager: Kaustubh Manglurkar
Publishing Product Manager: Bhavya Rao
Book Project Manager: Arul Viveaun
Senior Editor: Rakhi Patel
Technical Editor: K Bimala Singha
Copy Editor: Safis Editing
Indexer: Tejal Soni
Production Designer: Jyoti Kadam
Marketing Coordinator: Nivedita Pandey
First published: January 2025
Production reference: 1121224
Published by Packt Publishing Ltd.
Grosvenor House
11 St Paul’s Square
Birmingham
B3 1RB, UK
ISBN 978-1-83546-906-4
www.packtpub.com
I would like to dedicate this book, first and foremost, to God for granting me the privilege of sharing knowledge, learning, and for always being by my side, empowering me. I also want to express my gratitude to my wife, Juliana, for her unwavering support throughout this journey, even when it meant spending less time together. Her encouragement and understanding of the importance of this project were invaluable. To my children, Noah and Antonela, I cannot thank you enough for your enthusiasm and encouragement. Your excitement about this book, even when it meant sacrificing a few hours of playtime, has been a constant source of motivation. To my friends, who have always believed in me and provided strength, encouragement, and inspiration, I extend my heartfelt thanks. While I cannot name everyone individually, please know that each of you is represented here. I would also like to thank the entire Packt team for their unconditional support, patience, and guidance, which made this project a reality. Finally, to you, the reader—thank you for purchasing this book. It was prepared with great care to provide you with the knowledge you need to achieve your goals.
– Albert S. Tanure
Albert S. Tanure is a cloud computing, software development, and DevOps expert with over 20 years of experience, serving as a software engineer, software architect, cloud solutions architect, and enterprise architect. He is a specialist in Microsoft technologies, having worked with .NET Framework since version 1.1. Currently, he develops cloud-native solutions, defining architectures for global companies for worldwide environments. He has been a Microsoft MVP since 2018 in the development technology category, MCT, and he actively contributes to the technical community through videos on his YouTube channel, Code FC, in addition to writing articles and giving lectures at various face-to-face and online events.
Adnan Maqbool Khan is a seasoned .NET full stack developer with over a decade of experience in the industry. A self-taught technology enthusiast, he continuously explores cutting-edge developments in .NET and frontend frameworks, such as React, Vue.js, and Angular, with a keen interest in web development and distributed system architectures. In his role as an engineering lead at a prominent UK-based software company, Adnan uses his extensive technical expertise to advance projects and mentor his team. A dedicated contributor to the developer community on LinkedIn, Adnan is passionate about helping others succeed with C# and .NET. He also stays current with emerging technologies through extensive reading of technology books.
I would like to express my sincere gratitude to my parents and wife for their continued patience and support. Your contributions have been invaluable to my success.
Dmitry Slabko is an accomplished software developer with over 25 years of experience. He has worked in organizations of all sizes – small shops, start-ups, and large international companies. He has developed multiple projects, from ground zero to their successful release to clients. He has extensive experience in web and cloud projects and focuses on .NET technologies. Over the past few years, he has participated as a mentor in internal mentoring programs for software developers and published a few articles. He has assisted in reviewing the second edition of .NET Core in Action by Dustin Metzgar. He also serves as a judge in various hackathons to help young talents grow into real professionals.
Parthasarathy C Mandayam is a dynamic, result-oriented IT professional with 35 years of experience in IT. He is a .NET and SQL Server expert, with 20 years of experience in .NET and 25 years of experience in SQL Server. He has developed several cutting-edge ASP.NET websites for top US Fortune 500 companies such as Progressive Insurance, Eddie Bauer, Providence Health System, and Blue Cross Blue Shield.
In this first part, we will focus on the fundamentals of the ASP.NET Core 9 platform, learning about the principles of the platform and the new features in the version. We will also be preparing the development environment using development tools such as Visual Studio Code and Visual Studio, in addition to the Software Development Kit (SDK) that contains packages and CLI tools for implementing solutions. All the knowledge acquired in this first part is essential for the reader to be able to learn more clearly the other topics covered in the rest of the book.
This part contains the following chapters:
Chapter 1, Introducing ASP.NET Core 9 ConceptsChapter 2, Building Dynamic UIs with Razor Pages, MVC, and BlazorChapter 3, Crafting Web APIs for Service DeliveryChapter 4, Real-Time Interactions with SignalRBefore starting to develop solutions using the ASP.NET Core 9 platform, we must learn the fundamentals and how to prepare the environment, as well as become accustomed to the main concepts that will be used during the development process.
In this chapter, we’ll learn about how to prepare our development environment, understand the differences between .NET and .NET Framework, and see what’s new in ASP.NET Core 9.
We’ll cover the following topics in this chapter:
Why ASP.NET Core 9?Comparing .NET and .NET FrameworkPreparing our development environmentWhat’s new in ASP.NET Core 9?My purpose is to introduce you to the main concepts of ASP.NET Core 9 and how to use this powerful platform to deliver web-based applications. I will explain the fundamentals of the platform, giving you context about the difference between .NET and .NET Framework and how to prepare your own Windows, Mac, or Linux environment, which you are going to work on until the end of this book. Furthermore, we’ll learn the most important improvements on this platform that have seen an increase in new features over the years. Let’s start to learn the basics of ASP.NET Core 9.
To take advantage of all the knowledge that will be shared in this book, it is important that you have access to a computer, with administrative privileges, and also access to the internet.
Other necessary software will be shared in this chapter and the rest of the book where necessary.
You can find all code examples and other materials used in this book in the following repository: https://github.com/PacktPublishing/ASP.NET-Core-9.0-Essentials.
ASP.NET core is a platform that has existed since 2016 and has been constantly improved, allowing the development of high-performance, modern, and cloud-ready solutions.
A few years ago, it was only possible to develop solutions on the .NET platform using the Windows operating system. However, with the great market demand and the rapid evolution of technologies, Microsoft began a one-way process of restructuring and redesigning the platform, adopting the open source model, which gave the developer community the opportunity to adopt a robust development model, independent of the operating system.
The last major version of ASP.NET to run exclusively on the Windows operating system was 4.x, and after the redesign, the platform was renamed ASP.NET Core, which is currently in version 9 STS(Standard Term Support).
Recently, ASP.NET Core 9 has become an extremely rich platform, enabling the delivery of solutions for different types of purposes and, what’s more, with the full support and focus of the open source community.
Using ASP.NET Core 9 provides us with a rich tool with the following benefits:
The ability to develop web UI solutionsThe ability to develop web APIsInteroperability of operating systemsCloud-readinessHigh performanceIntegrations with modern client-side frameworksThe use of best practices and design standardsIt is a complete platform that unifies everything necessary for the development of rich solutions using the best practices, technologies, and other aspects, which you will learn about throughout the chapters of this book.
Performance improvements
ASP.NET Core 9 features several important performance improvements over ASP.NET Core 7, making it the best-performing version to date. Some important improvement points are as follows:
- Faster execution and startup: ASP.NET Core 9 is faster than the previous version. Every version have been receiving many contributions from the technical community. The et runtime, responsible for the application execution, has some improvements like loop optimizations, many other code generation and Garbage Collector improvements.
- Minimal API Performance: According to benchmarks, the new version of the Minimal API is 15% faster than the previous version and consumes 93% less memory.
- Native AOT (Ahead-of-Time) compilation: Expanded support for native AOT in ASP.NET Core 9 allows applications to be compiled to native code, reducing disk footprint, improving startup times, and decreasing memory consumption, which is ideal for cloud-native environments.
- ML.NET important improvements enabling integrations with machine-learning models with the version ML.NET 4.0, with additional tokenizer support, necessary for modern IA models.
- .NET Aspire The .Net Aspire was introduced on the previous version .Net 9 adding an improved cloud-ready stack for building observable, production ready, distributed applications in ASP.NET Core 9. Many concerns related to the cloud-native approach during the development phase was abstracted with .NET Aspire, combining a couple of Nuget Packages and projects templates.
- .NET MAUI: The .Net MAUI (Multi-platform Application UI) which provides an unified way to develop applications for platforms like Web and Mobile, including IOS and Android. .NET MAUI has quality improvements like test coverage, end to end scenario test, and bug fixes. Now, as part of the project templates of ASP.NET Core 9 there is a hybrid project including MAUI integrated with Blazor. With this project software engineers are able to delivery applications not only for web, but also for mobile and windows.
- Entity Framework Core: Entity Framework Core is one of the most powerful feature of .Net, providing a way to abstract the communication between applications and database, using an approach called ORM(Object Relational Model). In the new version some more features and improvements was added like a Azure Cosmos DB for NoSQL provider and capabilities to work with AOT.
- ASP.NET Core: The entire ASP.NET Core platform has many improvements on Blazor, SignalR, minimal APIs, authentication and authorization and better support to the OpenAPI.
- Swagger: One of the most famous libraries for generating API documentation, Swagger, from the Nuget package Swashbuckle.AspNetCore will no longer be part of the default API template of ASP.NET Core 9. This is due to the fact of reducing the dependence on this library in .NET projects and improving support for Open API, a language-agnostic and platform-neutral of web based APIs.
For more details, check out the following link: https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-9/.
However, before starting to prepare our environment and use the ASP.NET Core 9, in the next section, let’s understand how the platform has been evolving with collaboration between Microsoft and the open source community, by comparing the .NET platform and the .NET Framework.
Both .NET and .NET Framework (usually called Full Framework) have similarities – that is, they are platforms that allow us to deliver great solutions.
In general, the platform is a framework with a set of features that allows us to develop different types of applications. In February 2002, .NET Framework brought a new development model using a centralized platform, allowing us to develop applications for Windows and the web through technologies such as Windows Forms, ASP.NET Web Forms, ASP.NET MVC, as well as console applications, among some other extensions.
Since the first versions, it was already possible to develop applications in different languages such as C#, Visual Basic, and any other languages that implemented the .NET Framework specifications. However, it depended on the Windows operating system and its system APIs.
The evolution and redesign of the .NET Core platform has brought many benefits to the Microsoft ecosystem, while maintaining the main idea of being a unified platform for the development of robust solutions. It is possible to develop in languages such as C#, F#, or even Visual Basic.
The structuring and redesign meant that the .NET core platform, now called just .NET, was developed in a modularized manner and independent of the operating system, with the support of the open source community.
Today, the entire .NET platform ecosystem is maintained by the .NET Foundation (https://dotnetfoundation.org/), a non-profit, independent organization that supports the entire open source platform ecosystem. With that, new possibilities were created for the .NET community, including the reduction in the lead time for delivery of new versions of the framework with new features and bug fixes.
The new releases of the .NET platform are made available annually, every November, in STS(Standard Term Support) versions, launched in even years and receiving support for 18 months, or LTS(Long Term Support), launched in odd years and receiving support for three years. There are also monthly patch updates, which speed up the agile correction of problems and vulnerabilities, maintaining compatibility between each patch and eliminating the greater risk of updates.
Understanding the platform update process brings great benefits to development times, as updates can cause non-conformities in applications, generating several problems.
Microsoft provides a complete roadmap of features that are added to the platform, as well as the implementation of improvements and, most importantly, fixes of bugs and vulnerabilities. Keep the roadmap link as a favorite in your browser: https://github.com/MoienTajik/AspNetCore-Developer-Roadmap.
Now that we understand some of the platform’s basics, let’s prepare our development environment on different operating systems.
The .NET platform has a set of tools available to offer the best experience to developers, regardless of the operating system used.
ASP.NET Core 9 applications can be developed and run on Windows, Linux, and macOS operating systems.
Code snippets will be presented throughout the book in order to demonstrate the concepts of ASP.NET Core 9 through practical examples. All supporting material for the book can be found in the GitHub repository, the link to which can be found in the Technical requirements section.
In this section, we will configure our environment on the three operating systems and create our first ASP.NET Core 9 project, but first, let’s see what things we will require to get started.
We can develop ASP.NET Core 9 applications using any text editor and then compile the developed code using the SDK(Software Development Kit), which will be discussed in the next section.
Microsoft offers two code-editing tools, Visual Studio and Visual Studio Code.
Visual Studio Code is a rich, extensible, and lightweight code editor that makes it possible to develop any type of application. It is a free tool, has several extensions, is widely used by the community, and can run on any operating system.
Conversely, Visual Studio is a more robust version of the IDE, having several visual functionalities that support development, in addition to several tools such as application profiling and a rich debug tool. Visual Studio only runs on the Windows operating system and a license must be purchased. However, Microsoft offers a version of Visual Studio called Community that is free and, despite some limitations, offers an excellent development experience. For the remainder of this book, we use Visual Studio Code as the main code editor, because it is extensible and, most importantly, free.
Visual Studio for Mac
Visual Studio for Mac will not be continued by Microsoft, and its support will end on August 31, 2024.
When proceeding with the installation of the .NET platform on your machine, some questions may arise regarding the SDK and the runtime.
The SDK allows us to develop and run ASP.NET Core 9 applications, while the runtime only has the necessary dependencies to run the applications.
In general, we always choose to use the SDK on development machines; however, when hosting environments, only the runtime is required. We’ll discuss hosting applications in more detail in Chapter 10.
Along with .NET and/or Visual Studio Code, a CLI will also be installed that will be used extensively throughout this book.
The CLI is nothing more than software executed through the command line, allowing to you to execute tasks for different purposes, such as the following command:
dotnet new webapp --name hello-worldThe CLI command here is called dotnet. This command has some parameters, responsible for determining the type of task that will be executed.
In a nutshell, the previous command creates a new project (new) of type webapp with the name (--name) hello-world.
CLI tools bring great flexibility, avoid UI dependencies, are extensible, and allow us to use automation strategies through scripts.
Throughout the book, we will use some CLI commands to support solution development and learning.
In the next sections, we will look at installing the ASP.NET Core 9 SDKs for all three operating systems.
Windows offers some options for installing the SDK for the .NET platform:
Installation together with Visual Studio.Using the Windows package manager, Winget. The SDK can be installed by running the following command:winget install -e --id Microsoft.DotNet.SDK.9Via PowerShell.However, we will proceed with the installation through Visual Studio, but if you prefer to use other installation options, check out this link: https://learn.microsoft.com/en-us/dotnet/core/install/windows.
Installation through Visual Studio is very simple and involves just a few steps:
Go to https://visualstudio.microsoft.com/.Download Visual Studio for Windows and save it. After downloading it, run the following file: VisualStudioSetup.exe.After installation, locate the VisualStudioSetup.exe file, run it, and then click on Continue.Figure 1.1 – The Visual Studio Installer message
On the Workloads tab, select the ASP.NET and web development option:Figure 1.2 – Visual Studio installation options
Then, click on the install button and proceed with the installation.macOS offers an executable that allows you to follow a simple installation process:
Download the .NET platform from https://dotnet.microsoft.com/download/dotnet.Select the version best suited to your processor (ARM64 or x64)After downloading, run the installer and complete the steps to complete the installation.Supported versions
Microsoft does not support .NET in versions before 6. From version 6 (LTS) onwards, current Apple processors are supported.
For more details on installing on macOS, refer to this link: https://learn.microsoft.com/en-us/dotnet/core/install/macos.
.NET supports several versions of Linux. For the next steps, we will focus on the Ubuntu 22.04 version, and we will run the process based on the script provided by Microsoft:
Access the command terminal, and create a folder called dotnet-install in your home directory.Download the sh script, provided by Microsoft with the following command:wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.shYou will need to add permission to the script before running it with the command:chmod +x ./dotnet-install.shNow, run the installation command:./dotnet-install.sh --version latestThis command will install the latest SDK version.Dependencies
The .NET platform depends on some libraries that are specific to each version of Linux. For more details, see the following link: https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu#dependencies.
Next, let’s install Visual Studio Code.
Visual Studio Code is a great editor, and its installation is very simple.
Just go to https://code.visualstudio.com/download, download the version specific to your operating system, run the installer, and complete the steps.
When installed, Visual Studio Code also installs its CLI. On Windows and Linux systems, the CLI is usually automatically added to the system PATH environment variable. On macOS, additional configuration will be required. To do this, follow these steps:
Press the CMD + Shift + P keys.Type in the following command: Install 'code' command in PATHPress Enter, and the CLI will be added to the Path environment variable.The time has come to validate the functioning of the environment, and to do so, we will create our first project.
After installing the editor, code, and SDK, it’s time to create our first application and ensure that the environment is functional for the rest of the book.
In this step, we will use the Terminal or Bash, the dotnet CLI, and also Visual Studio Code as the IDE. We will create a simple web application using the command line.
To do so, open a command terminal or bash and follow these instructions:
In your preferred directory, create a folder called Projects:mkdir ProjectsAccess the created folder with the following command:cd projectsNow, run the following command:dotnet newThe dotnet new command needs some instructions for us to proceed with creating a project. When running it, the following instructions are displayed:
Figure 1.3 – Executing the dotnet new command
As in Visual Studio, the .NET CLI has some templates that are used when creating projects. You will be able to see the templates installed on your machine by typing the highlighted command:Figure 1.4 – The available dotnet templates
We will continue with the creation of a web application in the MVC template. To do this, run the following command:dotnet new mvc --name my-first-appThe previous command is basically a template for the type of project that will be created. In this case, we will use the MVC model, which is a project that uses the Model-View-Controller (MVC) architectural pattern. Don’t worry about these details at this point. We will learn more about the .NET CLI tool, project models, and the MVC model throughout the book.
Now, access the directory of the created application with the following command:cd my-first-appNext, let’s open Visual Studio Code to make some changes to the application’s source code with the following command:code .The previous command will open a new instance of Visual Studio Code in the previously created application directory.Figure 1.5 – My first app, opened on Visual Studio Code
In Visual Studio Code, locate the index.cshtml file in Views | the Home folder.Replace line 6 of the index.cshtml file with the following code, and then select File |ZSave, or just press Ctrl + S: <h1 class="display-4"> Welcome to the ASP.NET Core 9! </h1>Return to the terminal or bash and run the following command:dotnet runFigure 1.6 – Running the dotnet run command
If you can see a message like the one shown in Figure 1.6, then your environment is working correctly.
Now, access the browser and type the address presented, as in the http://localhost:5034 example.Note the address on your terminal, as the application execution port number may be different.
Figure 1.7 – The application running
If all the previous steps were executed successfully, it means that your code editor was correctly configured, as well as the .NET SDK.
If there is any problem, review the installation steps according to your operating system.
With our environment configured, we are ready to continue learning the platform and implementing the examples set out in the book.
As it is open source and independent of any operating system, the .NET platform has received several improvements in recent years, further improving the experience of developers and bringing several new features constantly requested by the community and, in addition, several improvements in performance and bug fixes.
Some improvements are as follows:
Native AOT: This option was introduced in version 7.0 of the framework to create a self-contained application, in a single file, adding support for the x64 processors and ARM64 architectures of macOS and greatly reducing the size of the file, reaching up to 50% reduction in Linux environments.Improvements in serialization libraries: Applications constantly interact with data in JSON and the System.Text.Json API, native to .NET, has been constantly revised and improved, avoiding dependence on third-party libraries and considerably improving its performance and support.Performance: There has been a performance improvement of approximately 15%. You can check this link for more performance improvement details: https://learn.microsoft.com/en-us/aspnet/core/release-notes/aspnetcore-8.0?view=aspnetcore-8.0.These and many other features can be consulted directly from the platform roadmap at the following link: https://github.com/dotnet/aspnetcore/issues/44984.
The .NET roadmap is very well maintained and updated by the community, together with Microsoft and the .NET Foundation. It is exactly this great support from the community, as well as the other companies and organizations involved, that has turned .NET into a powerful development platform for different purposes. By following the other need of the market, it has considerably improved the options for the development of web-based solutions, using different approaches to meet different needs.
In this chapter, we learned about the .NET platform and ASP.NET Core 9, available in the main operating systems. We learned the differences between the .NET and .NET Framework, in addition to learning about the process of updating the framework versions and the difference between the STS and LTS versions. We also configured our development environment on different operating systems and developed an ASP.NET MVC application, validating the entire working environment .
In Chapter 2, we’ll use our already configured environment to learn the different approaches and options available to develop UIs in ASP.NET Core 9.
ASP.NET Core 9 has a complete UI framework to suit different types of approaches and applications that allow the use of page rendering strategies on both the client side and server side. In this chapter, we will learn about the options available in the ASP.NET Core UI framework, in addition to understanding how to define the best option for each scenario.
Initially, we will understand some important concepts and then continue with a practical approach so that we are able to exercise the concepts learned.
In this approach, we will initially understand what the ASP.NET Core UI framework is, learning about the different approaches to rendering applications both on the server, using Razor Pages and ASP.NET MVC, and rendering on the client, using JavaScript frameworks. Finally, we will understand the power of merging different technologies into a hybrid solution that uses the best of both the client and the server.
In this chapter, we’re going to cover the following main topics:
Learning about the ASP.NET Core UIImplementing a UI server render model with Razor Pages and ASP.NET MVCExploring UI client rendering with Blazor and JavaScript frameworksWorking with hybrid solutionsYou can find all code examples and other materials used in this chapter in the following repository: https://github.com/PacktPublishing/ASP.NET-Core-9.0-Essentials
Dynamic web-based applications have a flow that basically involves two distinct “worlds” the frontend and the backend. In general, the frontend deals with the mechanisms that are visualized and allow interaction between users and the various functionalities proposed by the solution. Therefore, a frontend is made up of buttons, texts, lists, menus, images, and other aspects that together form the UI. The backend is the representation of the mechanisms used to allow dynamism to the frontend, according to user interaction. We will discuss more about backend-related aspects in Chapter 3.
Each of the terms expressed in the previous paragraph works in different contexts. The frontend usually runs on the client, which translates into the user’s browser. Client is a generic term that can express other types of user interaction. But in this case, we will talk about the client as the browser of your choice. The backend runs on the server, whether in a data center or even a cloud provider such as Azure.
Through user interactions in the UI, the user must communicate with the backend and subsequently be able to adapt to the server’s responses to deliver some type of response and interaction, again , to the user.
The browser basically uses three technologies: HTML, CSS, and JavaScript. HTML is static; that is, it is interpreted by the browser and then rendered in the form of a UI. CSS is responsible for making visual elements more attractive, defining colors, shadows, and formatting, among other design aspects. JavaScript is used to make static elements dynamic, and this includes, for example, making a call to a server, and depending on the result, modifying the user’s UI to adapt to the response of the processing carried out. JavaScript is responsible for increasing the ability of web-based applications to become dynamic and interactive. However, it can be complex to create and manipulate elements through JavaScript code and, in addition, to manage calls on the server. There are several excellent JavaScript frameworks available, such as Angular or React that allow you to develop richly dynamic UI solutions.
It is essential to have knowledge of UI technologies; however, in addition, we can benefit from an integrated development model that allows us to work on both the UI and the backend in a rich way.
ASP.NET Core 9 has a complete UI framework to meet any UI requirement for web applications and is fully integrated with the .NET platform, using best practices for separation of responsibilities, management, and maintenance, among other important aspects. Despite this, ASP.NET Core is not restricted to the use of the UI renderers proposed by the platform; it also integrates well with JavaScript frameworks and even allows you to use the best of the available options by adopting a hybrid approach.
However, before we move on to a more specific understanding of the options available in ASP.NET Core 9, let’s learn about important aspects related to web systems architecture.
There are different approaches to developing web-based solutions when it comes to the UI layer, even before talking about technologies.
There are basically two models, client-side and server-side, with their respective pros and cons. There is also a third possibility, which would be to use a hybrid approach.
In the client-side model, processing is done locally in the browser. In this way, all the HTML, CSS, JavaScript, and other assets are processed by the browser, responding to user stimuli. Interactions with users are handled locally through scripts containing UI-related logic without the need to request the server.
However, this approach depends on server resources, such as access to data, among other things, and so it is necessary to make frequent calls to the server in order to obtain information based on the user’s needs, which is then processed and presented in the UI.
In the server-side approach, all the processing is delegated to the server, which returns a customized HTML page ready to be rendered in the browser. The server processes all the necessary information, allows access to data, manages business logic, has the ability to abstract sensitive information using secrets, and delegates minimal processing to the client. However, if the server is not available, it will not be possible to use the system.
In the hybrid approach, there is a combination of the best of “both worlds.”
There are many JavaScript frameworks prepared to make web-based systems dynamic, providing a great user experience by processing resources on the client and, likewise, having the ability to interact with the server, delegating the processing of only relevant information to the UI rather than the entire processing of a page.
Fortunately, ASP.NET Core 9 is prepared for the development of web-based solutions in the different approaches mentioned. We’ll start to understand the first UI development model, using Razor Pages, in the next section.
ASP.NET Core 9 offers two powerful server-render models: Razor Pages and MVC. They are similar models, but MVC is more elaborate and implements the Model-View-Controller architectural design pattern—we’ll talk more about this in the ASP.NET Core MVC section. For now, let’s start learning about Razor Pages.
Razor Pages is a server-rendered framework that implements a page-based model. The page-based model basically contextualizes the implementation of a specific page, taking into account the UI and business logic, but correctly segregating responsibilities.
Razor is a markup language that acts similarly to a template engine and combines its use with HTML and C# code.
Razor’s origins
Razor’s development began in June 2010, but it was only released in January 2011 with MVC 3, as part of Microsoft Visual Studio 2010. Razor is a simple-syntax visualization engine.
This page-based development model brings great advantages, such as ease of creating and updating the UI; it’s testable, keeps UI and business logic separate, and although it has similarities to ASP.NET Core MVC, it’s simpler. Taking all these advantages into consideration, let’s create our first project using Razor Pages.
To create a Razor Pages project, you can use Visual Studio or the dotnetCLI tool.
Creation using Visual Studio is very simple; just open the IDE, select Create a new project, and then select the ASP.NET Core Web App template, as shown in Figure 2.1:
Figure 2.1 – Selecting a project template
Both Visual Studio and the CLI tool work with the concept of templates. With the .NET platform, it is possible to develop different types of projects, whether for web, Windows, or mobile. Each template creates a basic project structure.
Throughout the rest of the book, we will use the CLI tool to create projects, as well as other needs that we will discuss later. From now on, we will use the .NET CLI tool to create a Razor Pages project, as this tool offers us several benefits that we will discuss in the rest of the book.
When you install the .NET 9 SDK, a number of tools are available. The main tool we will use in this book is dotnet.
The dotnet CLI tool also has a concept of templates. To test this functionality, open your operating system’s Command Prompt and run the following command:
dotnet --version