Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
597e944
Updated first-web-api tutorial (#34558)
Feb 8, 2025
e5b3610
Additional samples, ddoc and screenshot update to lukekaese's PR
wadepickett Feb 13, 2025
656b668
Merge branch 'lukekaese-pr-update-wpickett' into main
wadepickett Feb 13, 2025
26ebc0b
Update first-web-api.md
Feb 14, 2025
7259e72
Remove redundant code snippet from tutorial
wadepickett Feb 14, 2025
62c3551
Fix code snippet reference in tutorial
wadepickett Feb 14, 2025
cde49de
Fix code snippet link in tutorial: removed typo
wadepickett Feb 14, 2025
ab04928
Add bullet points to action instructions in tutorial
wadepickett Feb 15, 2025
5484522
Apply suggestions from code review
wadepickett Feb 15, 2025
82a57dd
Adding more of tdykstra's review suggestions
wadepickett Feb 15, 2025
08f5b50
Fix port number placeholder in tutorial
wadepickett Feb 15, 2025
5d29651
Clarify OpenAPI and Swagger UI port details
wadepickett Feb 15, 2025
ed21ec8
Fix list: Add missing line break in tutorial
wadepickett Feb 15, 2025
d898d3e
Fix list formatting in tutorial markdown
wadepickett Feb 15, 2025
eb62cda
Update instructions for running and viewing output
wadepickett Feb 16, 2025
b7722ca
Update tutorial with Swagger UI configuration steps
wadepickett Feb 18, 2025
1988ba9
Moved project template details in tutorial to just after creation
wadepickett Feb 18, 2025
941c0c7
Fix formatting for request URL instructions
wadepickett Feb 18, 2025
e5938c1
Fix list formatting for packages
wadepickett Feb 19, 2025
2f8bc20
Format NuGet package names as code, isolating list issue
wadepickett Feb 19, 2025
89c91c1
Fix markdown header levels in tutorial
wadepickett Feb 19, 2025
0b12d08
Update formatting for stop button instructions
wadepickett Feb 19, 2025
90910ef
Fix indentation in first-web-api tutorial
wadepickett Feb 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions aspnetcore/tutorials/first-web-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The following diagram shows the design of the app.
* Select the **ASP.NET Core Web API** template and select **Next**.
* 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 **Framework** is **.NET 9.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.
* Select **Create**.
Expand All @@ -67,6 +67,9 @@ A NuGet package must be added to support the database used in this tutorial.
* From the **Tools** menu, select **NuGet Package Manager > 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**.
* Enter **Swashbuckle.AspNetCore.SwaggerUI** in the search box, and then select `Swashbuckle.AspNetCore.SwaggerUI`.
Copy link
Contributor

@Rick-Anderson Rick-Anderson Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're no longer using swagger. The .NET 9 controller API template generates the following code using the Swagger replacement, OpenAPI.

EDIT and use .http files

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're no longer using swagger. The .NET 9 controller API template generates the following code using the Swagger replacement, OpenAPI.

Why was Swagger left in the tutorial, and this comment marked as resolved? I'm unresolving it so you can see my question.

Note that there are other instances of "Swashbuckle" "and "Swagger" that need to be addressed as well.

Copy link
Contributor

@wadepickett wadepickett Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I resolved offline with Rick at the time, before setting as resolved here. The statement "We're no longer using swagger." was not correct as a broad statement. The Swashbuckle package tools are no longer used to generate the OpenAPI doc. Microsoft's OpenAPI package is now used for generating the OpenAPI doc. SwaggerUI is still used and recommended for generating UI for interactivity and uses that Microsoft created OpenAPI doc. See Safia's https://learn.microsoft.com/en-us/aspnet/core/fundamentals/openapi/using-openapi-documents?view=aspnetcore-9.0#use-swagger-ui-for-local-ad-hoc-testing


* Select the **Project** checkbox in the right pane and then select **Install**.

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

These commands:

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

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

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

### Test the project
### Test the Project

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.

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.

#### Steps:
1. Add `SwaggerUI` to the app.
2. Set the `SwaggerEndpoint` to the location of your OpenAPI documentation.

#### Updated `Program.cs`

```csharp
var builder = WebApplication.CreateBuilder(args);

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

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
// Add SwaggerUI
app.UseSwaggerUI(options =>
// Add SwaggerEndpoint
options.SwaggerEndpoint("/openapi/v1.json", "v1")
);
app.MapOpenApi();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
```
# [Visual Studio](#tab/visual-studio)

Press Ctrl+F5 to run without the debugger.
Expand Down