Skip to content

Commit 597e944

Browse files
author
Lukas Pilney
committed
Updated first-web-api tutorial (#34558)
1 parent b45aab3 commit 597e944

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

aspnetcore/tutorials/first-web-api.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The following diagram shows the design of the app.
5555
* Select the **ASP.NET Core Web API** template and select **Next**.
5656
* In the **Configure your new project dialog**, name the project *TodoApi* and select **Next**.
5757
* In the **Additional information** dialog:
58-
* Confirm the **Framework** is **.NET 8.0 (Long Term Support)**.
58+
* Confirm the **Framework** is **.NET 9.0 (Long Term Support)**.
5959
* Confirm the checkbox for **Use controllers(uncheck to use minimal APIs)** is checked.
6060
* Confirm the checkbox for **Enable OpenAPI support** is checked.
6161
* Select **Create**.
@@ -67,6 +67,9 @@ A NuGet package must be added to support the database used in this tutorial.
6767
* From the **Tools** menu, select **NuGet Package Manager > Manage NuGet Packages for Solution**.
6868
* Select the **Browse** tab.
6969
* Enter **Microsoft.EntityFrameworkCore.InMemory** in the search box, and then select `Microsoft.EntityFrameworkCore.InMemory`.
70+
* Select the **Project** checkbox in the right pane and then select **Install**.
71+
* Enter **Swashbuckle.AspNetCore.SwaggerUI** in the search box, and then select `Swashbuckle.AspNetCore.SwaggerUI`.
72+
7073
* Select the **Project** checkbox in the right pane and then select **Install**.
7174

7275
# [Visual Studio Code](#tab/visual-studio-code)
@@ -79,13 +82,14 @@ A NuGet package must be added to support the database used in this tutorial.
7982
dotnet new webapi --use-controllers -o TodoApi
8083
cd TodoApi
8184
dotnet add package Microsoft.EntityFrameworkCore.InMemory
85+
dotnet add package Swashbuckle.AspNetCore.SwaggerUI
8286
code -r ../TodoApi
8387
```
8488

8589
These commands:
8690

8791
* Create a new web API project and open it in Visual Studio Code.
88-
* Add a NuGet package that is needed for the next section.
92+
* Add a NuGet packages that are needed for the next section.
8993
* Open the *TodoApi* folder in the current instance of Visual Studio Code.
9094

9195
[!INCLUDE[](~/includes/vscode-trust-authors-add-assets.md)]
@@ -94,10 +98,47 @@ A NuGet package must be added to support the database used in this tutorial.
9498

9599
[!INCLUDE[](~/includes/package-reference.md)]
96100

97-
### Test the project
101+
### Test the Project
102+
103+
The project template creates a `WeatherForecast` API with OpenAPI JSON documentation. By default, you can access the documentation while the project is running by navigating your browser to `https://localhost:<port>/openapi/v1.json`, where `<port>` is a randomly chosen port number set during project creation.
104+
105+
To use the [Swagger](xref:tutorials/web-api-help-pages-using-swagger) UI for testing the API, you need to modify the `Program.cs` file in your project.
106+
107+
#### Steps:
108+
1. Add `SwaggerUI` to the app.
109+
2. Set the `SwaggerEndpoint` to the location of your OpenAPI documentation.
110+
111+
#### Updated `Program.cs`
112+
113+
```csharp
114+
var builder = WebApplication.CreateBuilder(args);
98115

99-
The project template creates a `WeatherForecast` API with support for [Swagger](xref:tutorials/web-api-help-pages-using-swagger).
116+
// Add services to the container.
117+
builder.Services.AddControllers();
118+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
119+
builder.Services.AddOpenApi();
100120

121+
var app = builder.Build();
122+
123+
// Configure the HTTP request pipeline.
124+
if (app.Environment.IsDevelopment())
125+
{
126+
// Add SwaggerUI
127+
app.UseSwaggerUI(options =>
128+
// Add SwaggerEndpoint
129+
options.SwaggerEndpoint("/openapi/v1.json", "v1")
130+
);
131+
app.MapOpenApi();
132+
}
133+
134+
app.UseHttpsRedirection();
135+
136+
app.UseAuthorization();
137+
138+
app.MapControllers();
139+
140+
app.Run();
141+
```
101142
# [Visual Studio](#tab/visual-studio)
102143

103144
Press Ctrl+F5 to run without the debugger.

0 commit comments

Comments
 (0)