Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Web API Development with ASP.NET Core 8
  • Table Of Contents Toc
  • Feedback & Rating feedback
Web API Development with ASP.NET Core 8

Web API Development with ASP.NET Core 8

By : Yan
4.5 (16)
close
close
Web API Development with ASP.NET Core 8

Web API Development with ASP.NET Core 8

4.5 (16)
By: Yan

Overview of this book

Web API applications have become increasingly significant in recent years, fueled by the ever-accelerating pace of technological advancements. However, with this rapid evolution comes the need to create web API apps that are not only functional but also adaptable, maintainable, and scalable to meet the demands of users and businesses alike. This book draws from the author’s immense technical expertise and decades of experience in software development to help you address this challenge head-on, equipping you with the knowledge and skills required to develop web API apps from scratch. By providing a deeper understanding of the various protocols implemented by ASP.NET Core, including RESTful, SignalR (WebSocket), gRPC, and GraphQL, supplemented by practical examples and optimization techniques, such as using middleware, testing, caching, and logging, this book offers invaluable insights for both newcomers as well as seasoned developers to meet modern web development requirements. Additionally, you’ll discover how to use cloud platforms such as Azure and Azure DevOps to enhance the development and operational aspects of your application. By the end of this book, you’ll be fully prepared to undertake enterprise-grade web API projects with confidence, harnessing the latest advancements in ASP.NET Core 8 to drive innovation.
Table of Contents (20 chapters)
close
close

Creating a simple REST web API project

In this section, we will use the .NET command-line interface (.NET CLI) to create a basic web API project and see how it works.

The .NET CLI is a command-line tool that helps you to create, develop, build, run, and publish .NET applications. It is included in the .NET SDK.

You have multiple ways to run .NET CLI commands. The most common way is to run the command in the terminal window or command prompt. Also, you can run the command in VS Code directly. VS Code provides an integrated terminal that starts at the root of your workspace. To open the terminal in VS Code, you can do any one of the following:

  • Press Ctrl + ` (on Windows) or Command + ` (on macOS) to open the terminal
  • Use the View | Terminal menu item to open the terminal
  • From the Command Palette, use the View: Toggle Terminal command to open the terminal

In the terminal, navigate to a folder where you want to create the project, then create a web API project by running the following command:

dotnet new webapi -n MyFirstApi -controllers
cd MyFirstApi
code .

The preceding commands create a new web API project and open it in VS Code. dotnet new provides many options to create various types of projects, such as web APIs, console apps, class libraries, and so on.

There are some options we can use to specify the project:

  • -n|--name <OUTPUT_NAME>: The name for the created output. If not specified, the name of the current directory is used.
  • -o|--output <OUTPUT_PATH>: The output path for the created project. If not specified, the current directory is used.
  • -controllers|--use-controllers: Indicates whether to use controllers for actions. If not specified, the default value is false.
  • -minimal|--use-minimal-apis: Indicates whether to use minimal APIs. The default value is false, but the -controllers option will override the -minimal option. If neither -controllers nor -minimal is specified, the default value of the -controllers option, which is false, will be used, so a minimal API will be created.

Important note

Since .NET 6.0, ASP.NET Core 6.0 provides a new way to create web API projects, which is called minimal APIs. It is a simplified approach for building APIs without controllers. We will introduce minimal APIs later. For now, we will use the traditional way to create a web API project with controllers. So, we need to specify the --use-controllers option.

To learn more about the dotnet new command, check this page: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new. We will introduce more details on the dotnet command in the following sections.

When you use VS Code to open the project, the C# Dev Kit extension can create a solution file for you. This feature makes VS Code more friendly to C# developers. You can see the following structure in the Explorer view:

The reason is that VS 2022 will create a sln file for the project, but .NET CLI does not. When using VS Code to open the project, the C# DevKit will create the sln file. I think it's worth mentioning it here.

The C# Dev Kit extension provides a new feature, the solution explorer, which is located at the bottom. This feature is especially useful when working with multiple projects in one solution. You can drag and drop the SOLUTION EXPLORER to the top to make it more visible.

When you use VS Code to open the project, the C# Dev Kit extension can create a solution file for you. This feature makes VS Code more friendly to C# developers. You can see the following structure in the Explorer view:

Figure 2.2 – The solution explorer and the folder structure

Figure 2.2 – The solution explorer and the folder structure

Next, we can start to build and run the project.

Create a Note

Modal Close icon
You need to login to use this feature.
notes
bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Delete Note

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Edit Note

Modal Close icon
Write a note (max 255 characters)
Cancel
Update Note

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY