Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated first-web-api tutorial #34676

Closed
wants to merge 23 commits into from
Closed

Updated first-web-api tutorial #34676

wants to merge 23 commits into from

Conversation

lukekaese
Copy link

@lukekaese lukekaese commented Feb 8, 2025

Hi,

A colleague of mine recently took his first steps with C# and ASP.NET Core, specifically in .NET 9. Unfortunately, he became quite frustrated while following this tutorial:

Create a web API with ASP.NET Core

The tutorial did not work as expected. Since I work full-time with C# in our company, I stepped in to assist, but I noticed that the tutorial does not function correctly in its current state. Either the mention of Swagger needs to be removed, or the tutorial should be updated to reflect the necessary adjustments.

I found that only a few lines of code need modification to restore the expected functionality. I also came across issue #34558, which might be related, so I wanted to contribute a fix.

I hope this helps! Let me know if any further adjustments are needed.

Best regards,
Luke

[Added below by Wade Pickett]

Fixes #33001


Internal previews

📄 File 🔗 Preview link
aspnetcore/tutorials/first-web-api.md aspnetcore/tutorials/first-web-api

@lukekaese
Copy link
Author

@dotnet-policy-service agree

@guardrex guardrex requested a review from wadepickett February 8, 2025 11:53
@wadepickett
Copy link
Contributor

@likekaese, thanks so much for your contribution, and I am sorry to hear of your colleague's experience.

This work for this was scheduled for this month's "sprint" and tracked in an issue here:
#33001

I wasn't going to hit this one until other work was completed this coming week, but I'll move it up since you are already starting, and I want you to be successful with the work you already put into it. Thanks very much for your start!

We usually try to keep code samples in a self-contained project directory for each version rather than putting the code inline to the .md doc itself. I already had a directory set up for that some time ago before I had to move over to higher priority work a while back here, but it wasn't updated yet:
https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/tutorials/first-web-api/samples

So next you will see the app samples replaced in code that included from the project samples.
We also had some guidance to add.

I will keep what I can of your work here so you get credit, but you will see some changes coming in soon (Today is Sunday and I might not be able to get a lot done until Monday).

Nothing for you to do at the moment as I am making those changes.

Thanks again for the contribution, greatly appreciated!

@wadepickett wadepickett self-assigned this Feb 9, 2025
@lukekaese
Copy link
Author

@wadepickett Thanks, I'm always happy to contribute! Good to know this was already planned, and I really appreciate you prioritizing it. Keeping code samples in separate project directories makes sense—I’ll keep that in mind for future contributions (and maybe some hobby projects). Looking forward to seeing how everything comes together. Let me know if there's anything else I can do. Thanks again!

@@ -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.

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

@wadepickett
Copy link
Contributor

wadepickett commented Feb 12, 2025

@lukekaese, please don't make any changes or close. I'm in the middle of updating this PR with work I finished last night. I think there is some miscommunication around using SwaggerUI for visualization and interaction while OpenAPI generates the doc that SwaggerUI uses to provide visualization (What I am updating the PR for) vs using the Swashbuckle tools to generate the doc which, we would not do and the point I think Rick is raising. Sorting it out with Rick. Thanks.

@wadepickett wadepickett changed the title Updated first-web-api tutorial (#34558) Updated first-web-api tutorial Feb 12, 2025
@lukekaese
Copy link
Author

@wadepickett I wasn't going to, but I was about to say something along those lines ^^. Perhaps my wording was a bit unclear, but yes, exactly as you said: OpenAPI is for generating, while SwaggerUI is for visualizing.

@wadepickett
Copy link
Contributor

No worries, I understood.
Sorry for the delay and thanks for your patience. I will have a commit in here tonight further updating the doc, screenshots and app samples.

@wadepickett wadepickett changed the base branch from main to lukekaese-pr-update-wpickett February 13, 2025 21:09
@wadepickett
Copy link
Contributor

Moving as approved to new branch, then adding my changes in a moment...

Copy link
Contributor

@wadepickett wadepickett left a comment

Choose a reason for hiding this comment

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

Approving work so far so I can move this into a new branch and add changes from there.

@wadepickett
Copy link
Contributor

Tab issue in doc, fixing it...

@wadepickett
Copy link
Contributor

Close and reopening pr to flush cache.

@wadepickett wadepickett reopened this Feb 14, 2025
@wadepickett wadepickett marked this pull request as draft February 14, 2025 04:45
@wadepickett
Copy link
Contributor

@lukekaese please do not make any changes. Will let you know when it is in a state ready for review of my changes.

@CamSoper CamSoper changed the base branch from lukekaese-pr-update-wpickett to main February 14, 2025 19:47
@wadepickett wadepickett marked this pull request as ready for review February 15, 2025 00:25
@wadepickett wadepickett requested a review from tdykstra February 15, 2025 00:25
@wadepickett
Copy link
Contributor

@tdykstra, I added a lot to this original PR for the update. It would be good to get a review from you since I added so much.
aspnetcore/tutorials/first-web-api

Copy link
Contributor

@tdykstra tdykstra left a comment

Choose a reason for hiding this comment

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

I reviewed and executed the instructions to validate them up to scaffolding the controller and skimmed the changes after that.

I thought we were going to use .http files for testing with VS in .NET 9 (?)

* `Microsoft.EntityFrameworkCore.InMemory`: Enables Entity Framework Core to work with an in-memory database rather than an external one, simplifying this tutorial.
* `Swashbuckle.AspNetCore.SwaggerUI`: Provides a user interface for exploring and testing API endpoints interactively through Swagger.

Add the following NuGet packages used in this tutorial.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not rendering as a bulleted list.

Copy link
Contributor

@wadepickett wadepickett Feb 18, 2025

Choose a reason for hiding this comment

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

Thanks for catching this. Trying to figure out what is wrong with it. It looks right in markdown.

Copy link
Contributor

Choose a reason for hiding this comment

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

@tdykstra, I cannot figure out why a blank line is showing up between the list items.

This for example, renders fine as a list:

The preceding highlighted code:

  • Adds the Swagger UI as a service to the app with app.UseSwaggerUI().
  • Sets the SwaggerEndpoint() option to the location of the OpenAPI documentation for this project.
  • Ensures the Swagger UI is only available in the app development environment to limit information disclosure and security vulnerability.

This, which is set up the same way, throws in line spaces in the build, but does fine when displayed here:

This tutorial uses the following additional NuGet packages:

  • Microsoft.EntityFrameworkCore.InMemory: Enables Entity Framework Core to work with an in-memory database rather than an external one, simplifying this tutorial.
  • Swashbuckle.AspNetCore.SwaggerUI: Provides a user interface for exploring and testing API endpoints interactively through Swagger.

I can't spot what is tripping it up. I also checked against the style guide examples and other topics we have here doing the same thing. I tried removing the code ticks ` just in case there was some issue with that. That wasn't it either.

@@ -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

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.

@wadepickett
Copy link
Contributor

wadepickett commented Feb 15, 2025

I reviewed and executed the instructions to validate them up to scaffolding the controller and skimmed the changes after that.

I thought we were going to use .http files for testing with VS in .NET 9 (?)

@tdykstra, thanks.
This is the way and order Safia presented options here for .NET 9: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/openapi/using-openapi-documents?view=aspnetcore-9.0

  • SwaggerUI (She installs it through the Swashbuckle.AspNetCore.SwaggerUi package )
  • ReDocs
  • Scalar
  • Spectral

SwaggerUI is currently a heavily poplular tool out there for visualization/interactivity. We have examples in tutorials now using VS .http and Scalar, but I decided to maintain this one as the remaining SwaggerUI example. ( SwaggerUI using Microsofts OpenAPI package that creates the doc, which SwaggerUI uses to add the visualization. )

That seem good?

Also see my explanation on Swagger UI in the other comment above and that I had resolved with Rick offline before marking that item as resolved.

@wadepickett wadepickett requested a review from tdykstra February 19, 2025 04:01
@wadepickett
Copy link
Contributor

Sorry @lukekaese and @tdykstra, closing per #34746 (comment)

timdeschryver added a commit to timdeschryver/AspNetCore.Docs that referenced this pull request Feb 19, 2025
@wadepickett
Copy link
Contributor

Reopening to get a build and will close back up.

@wadepickett wadepickett reopened this Feb 19, 2025
@wadepickett
Copy link
Contributor

Closing per note above.

@lukekaese
Copy link
Author

@wadepickett don't worry. I'm sorry for wasting your time for a bit. At least the Documentation is getting better, That's all that counts. Maybe my next PR is more successful. Thanks you for everything ^^

wadepickett added a commit that referenced this pull request Feb 20, 2025
* Update .NET version references in first-web-api.md to 9.0

* update sample to target .NET 9.0

* rename example to _SwaggerExample

* add OpenAPI example (no swagger)

* update WebApi to .NET 9 (without swagger)

* remove unused example

* review fixes

* apply changes from #34676

* include IsDevelopment codeblock to match the text description

* VS new project settings moved to order they appear in dialog

* Right-click or Command-click on project in VSC

Control-click on a project in VSC on windows does not work, must be Right-click for Windows and Control-click on macOS for VSC.

* Specifying the DTO file and location in the instruction

* Apply suggestions from code review

* Update author names in web API tutorial

* Add comment block in first-web-api tutorial

* Apply suggestions from code review

revert

* Update aspnetcore/tutorials/first-web-api.md

* remove unused file 'create-new-project'

---------

Co-authored-by: Wade Pickett <wpickett@microsoft.com>
Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create Web API: v9 update sample update and rewrite for VSC version OpenAPI UI gen
5 participants