This tutorial teaches the basics of building a controller-based web API that uses a database. For help with choosing between minimal APIs and controller-based APIs, see APIs overview. The following diagram shows the design of the app. You can follow these instructions on macOS, Linux, or Windows and with any code editor. Minor changes may be required if you use something other than Visual Studio Code. Microsoft has announced the retirement of Visual Studio for Mac. Dev Kit and related extensions, such as .NET MAUI and Unity. Visual Studio IDE running on Windows in a VM on Mac. Visual Studio IDE running on Windows in a VM in the Cloud. For more information, see Visual Studio for Mac retirement announcement. Enter Web API in the search box. In the Configure your new project dialog, name the project TodoApi and select Next. In the Additional information dialog: - Confirm the Framework is .NET 8.0 (Long Term Support).
Confirm the checkbox for Use controllers(uncheck to use minimal APIs) is checked. Confirm the checkbox for Enable OpenAPI support is checked. A NuGet package must be added to support the database used in this tutorial. Manage NuGet Packages for Solution. Select the Browse tab. Enter Microsoft.EntityFrameworkCore.InMemory in the search box, and then select Microsoft.EntityFrameworkCore.InMemory. Select the Project checkbox in the right pane and then select Install. Open the integrated terminal. Change directories (cd) to the folder that will contain the project folder. Create a new web API project and open it in Visual Studio Code. Add a NuGet package that is needed for the next section. Open the TodoApi folder in the current instance of Visual Studio Code. Visual Studio Code might display a dialog box that asks: Do you trust the authors of the files in this folder? If you trust all files in the parent folder, select Trust the authors of all files in the parent folder. When Visual Studio Code requests that you add assets to build and debug the project, select Yes.
Command Palette and type ".NET" into the search box. From the list of commands, select the .NET: Generate Assets for Build and Debug command. Visual Studio Code adds a .vscode folder with generated launch.json and tasks.json files. Confirm the checkbox for Enable OpenAPI support is checked. Confirm the checkbox for Use controllers is checked. In the search box, enter Microsoft.EntityFrameworkCore.InMemory. In the results window, check Microsoft.EntityFrameworkCore.InMemory. In the Select Projects window, select Ok. In the License Agreement window, select Agree. For guidance on adding packages to .NET apps, see the articles under Install and manage packages at Package consumption workflow (NuGet documentation). The project template creates a WeatherForecast API with support for Swagger. F5 to run without the debugger. Select Yes if you trust the IIS Express SSL certificate. Select Yes if you agree to trust the development certificate. Visual Studio launches the default browser and navigates to https://localhost:/swagger/index.html, where is a randomly chosen port number set at the project creation.
The preceding command doesn't work on Linux. See your Linux distribution's documentation for trusting a certificate. Select Yes if you agree to trust the development certificate. HTTPS URL in the output to test the web app in a browser. The default browser is launched to https://localhost:/swagger/index.html, where is the randomly chosen port number displayed in the output. There's no endpoint at https://localhost:, so the browser returns HTTP 404 Not Found. Append /swagger to the URL, https://localhost:/swagger. C in the integrated terminal to shut it down. Start Debugging to launch the app. Visual Studio for Mac launches a browser and navigates to https://localhost:/swagger/index.html, where is a randomly chosen port number set at the project creation. The Swagger page /swagger/index.html is displayed. The Curl command to test the WeatherForecast API. The URL to test the WeatherForecast API. The response code, body, and headers. A drop-down list box with media types and the example value and schema. If the Swagger page doesn't appear, see this GitHub issue.
|