3 Creating web ser vices and applications with ASP.NET Core

 

This chapter covers

  • Creating web services
  • Creating web applications
  • Using Language Integrated Query (LINQ)

The console application from chapter 2 receives command-line parameters and writes the specified string in ASCII art to the console output. The command-line library parses the parameters using common conventions, making the application easy to execute from a terminal. Creating a web service that performs the same function is just as easy.

3.1 Web services

In this section, we’ll port the ASCII art program into a web service using ASP.NET Core. In chapter 2, we used the dotnet new console command to create a console application from a template. Many other templates are available. You can see a list by running dotnet new list (or, in .NET 7 or earlier, dotnet new --list). We will use the empty web template for the ASCII art service. Use the command shown in the following listing to create a new folder with the new project.

Listing 3.1 Command to create web API from template
dotnet new web --name AsciiArtSvc

This template will create files similar to the ones shown in figure 3.1.

Figure 3.1 Files and folders created by the empty web template

A web API template will look much more like the ASP.NET Core projects you see in the real world. We use the empty template in this example because with the minimal API design of ASP.NET Core, creating a web service is as easy as creating a console application.

3.1.1 Adding a service that responds with a collection of data

3.1.2 Controlling the response

3.2 Web applications

3.2.1 Razor pages

3.2.2 Code-behind

Summary