Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals - Eighth Edition

You're reading from  C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals - Eighth Edition

Product type Book
Published in Nov 2023
Publisher Packt
ISBN-13 9781837635870
Pages 828 pages
Edition 8th Edition
Languages
Author (1):
Mark J. Price Mark J. Price
Profile icon Mark J. Price

Table of Contents (18) Chapters

Preface 1. Hello, C#! Welcome, .NET! 2. Speaking C# 3. Controlling Flow, Converting Types, and Handling Exceptions 4. Writing, Debugging, and Testing Functions 5. Building Your Own Types with Object-Oriented Programming 6. Implementing Interfaces and Inheriting Classes 7. Packaging and Distributing .NET Types 8. Working with Common .NET Types 9. Working with Files, Streams, and Serialization 10. Working with Data Using Entity Framework Core 11. Querying and Manipulating Data Using LINQ 12. Introducing Web Development Using ASP.NET Core 13. Building Websites Using ASP.NET Core Razor Pages 14. Building and Consuming Web Services 15. Building User Interfaces Using Blazor 16. Epilogue 17. Index

Building Websites Using ASP.NET Core Razor Pages

This chapter is about building websites with a modern HTTP architecture on the server side using Microsoft ASP.NET Core. You will learn about building simple websites using the ASP.NET Core Razor Pages feature introduced with ASP.NET Core 2 and the Razor class library feature introduced with ASP.NET Core 2.1.

This chapter covers the following topics:

  • Exploring ASP.NET Core
  • Exploring ASP.NET Core Razor Pages
  • Using Entity Framework Core with ASP.NET Core
  • Configuring services and the HTTP request pipeline

Exploring ASP.NET Core

We will start by creating an empty ASP.NET Core project and explore how to enable it to serve simple web pages.

Creating an empty ASP.NET Core project

We will create an ASP.NET Core project that will show a list of suppliers from the Northwind database.

The dotnet tool has many project templates that do a lot of work for you, but it can be difficult to know which works best for a given situation, so we will start with the empty website project template and then add features step by step so that you can understand all the pieces:

  1. Use your preferred code editor to open the PracticalApps solution and then add a new project, as defined in the following list:
    • Project template: ASP.NET Core Empty [C#] / web. For JetBrains Rider, select the project template named ASP.NET Core Web Application, and then set Type to Empty.
    • Project file and folder: Northwind.Web.
    • Solution file and folder: PracticalApps.
    • For...

Exploring ASP.NET Core Razor Pages

ASP.NET Core Razor Pages allow a developer to easily mix C# code statements with HTML markup to make the generated web page dynamic. That is why Razor Pages use the .cshtml file extension.

By convention, ASP.NET Core looks for Razor Pages in a folder named Pages.

Enabling Razor Pages

You will now copy and change the static HTML page into a dynamic Razor Page, and then add and enable the Razor Pages service:

  1. In the Northwind.Web project folder, create a folder named Pages.
  2. Copy the index.html file into the Pages folder. (In Visual Studio 2022 or JetBrains Rider, hold down Ctrl while dragging and dropping.)
  3. For the file in the Pages folder, rename the file extension for index.html from .html to .cshtml.
  4. In the Pages folder, in index.cshtml, add the @page directive to the top of the file.
  5. In index.cshtml, remove the <h2> element that says that this is a static HTML page.
  6. In Program.cs, after...

Using Entity Framework Core with ASP.NET Core

Entity Framework Core is a natural way to get real data onto a website. In Chapter 12, Introducing Web Development Using ASP.NET Core, you created two pairs of class libraries: one for the entity models and one for the Northwind database context, for SQL Server and SQLite. You will now use them in your website project.

Configuring Entity Framework Core as a service

Functionality, such as Entity Framework Core database contexts, that is needed by an ASP.NET Core project should be registered as a dependency service during website startup. The code in the GitHub repository solution and below uses SQLite, but you can easily use SQL Server if you prefer.

Let’s see how:

  1. In the Northwind.Web project, add a project reference to the Northwind.DataContext project for either SQLite or SQL Server, as shown in the following markup:
    <!-- Change Sqlite to SqlServer if you prefer. -->
    <ItemGroup>
      <...

Configuring services and the HTTP request pipeline

Now that we have built a website that reads and writes to a database, we will return to the website configuration and review how services and the HTTP request pipeline work in more detail.

Understanding endpoint routing

Endpoint routing is designed to enable better interoperability between frameworks that need routing, such as Razor Pages, MVC, or Web APIs, and middleware that needs to understand how routing affects them, such as localization, authorization, and so on.

Endpoint routing gets its name because it represents the route table as a compiled tree of endpoints that can be walked efficiently by the routing system. One of the biggest improvements is the performance of routing and action method selection.

Configuring endpoint routing

For more complex scenarios than we have seen so far, endpoint routing can use a pair of calls to the UseRouting and UseEndpoints methods:

  • UseRouting marks the pipeline...

Join our book community on Discord

https://packt.link/EarlyAccess

Qr code Description automatically generated

The third and final part of this book is about web development using ASP.NET Core. You will learn how to build cross-platform projects such as websites, web services, and web browser apps.Microsoft calls platforms for building applications app models or workloads. I recommend that you work through this and subsequent chapters sequentially because later chapters will reference projects in earlier chapters, and you will build up sufficient knowledge and skills to tackle the trickier problems in later chapters.In this chapter, we will cover the following topics:

  • Understanding ASP.NET Core
  • New features in ASP.NET Core
  • Structuring projects
  • Building an entity model for use in the rest of the book
  • Understanding web development

Understanding ASP.NET Core

Since this book is about C# and .NET, we will learn about app models that use them to build the practical applications that we will encounter in the remaining chapters of...

12 Introducing Web Development Using ASP.NET Core

Join our book community on Discord

https://packt.link/EarlyAccess

Qr code Description automatically generated

The third and final part of this book is about web development using ASP.NET Core. You will learn how to build cross-platform projects such as websites, web services, and web browser apps.Microsoft calls platforms for building applications app models or workloads. I recommend that you work through this and subsequent chapters sequentially because later chapters will reference projects in earlier chapters, and you will build up sufficient knowledge and skills to tackle the trickier problems in later chapters.In this chapter, we will cover the following topics:

  • Understanding ASP.NET Core
  • New features in ASP.NET Core
  • Structuring projects
  • Building an entity model for use in the rest of the book
  • Understanding web development

Understanding ASP.NET Core

Since this book is about C# and .NET, we will learn about app models that use them to build the practical applications that...

lock icon The rest of the chapter is locked
You have been reading a chapter from
C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals - Eighth Edition
Published in: Nov 2023 Publisher: Packt ISBN-13: 9781837635870
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}