You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 4-minimal-api/README.md
+91-38Lines changed: 91 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ Because these things are so common, the web was designed with standard commands
20
20
21
21
To avoid reinventing things over and over, we use some common standards for HTTP APIs. We’ve already talked about HTTP, which handles the commands and communication between clients and servers. Another important thing for HTTP APIs to do in a predictable way is to send data back and forth. The most common way to package up data now is called JSON, short for JavaScript Object Notation. It’s a neat, simple format for packaging up data, but programmers don’t want to spend time converting their information back and forth between properties and classes in their programming language and JSON. This conversion process is called serialization, and fortunately ASP.NET Core can do that for you automatically.
22
22
23
-
### Welcome to the internet...
23
+
### Welcome to the internet
24
24
25
25
Another important thing to think about for server exposed to the public internet is security and authorization. Some HTTP APIs require authentication, so only clients with the right credentials can access them. Even public HTTP APIs need to handle security, to manage things like denial of service attacks and exploiting the public APIs to take over the server or get access to information they shouldn’t have. Fortunately, ASP.NET Core can handle things like this for us, too.
26
26
@@ -47,12 +47,12 @@ This endpoint accepts a pizza JSON object, turns it into C#, and passes it to a
47
47
48
48
### Create a new Minimal API project
49
49
50
-
First, you need to scaffold a project. You've installed .NET 6 and you're ready to go.
50
+
First, you need to scaffold a project. You've installed .NET 9 and you're ready to go.
51
51
52
52
1. Create a web API by running `dotnet new`:
53
53
54
54
```bash
55
-
dotnet new web -o PizzaStore -f net6.0
55
+
dotnet new web -o PizzaStore -f net9.0
56
56
```
57
57
58
58
You should see the _PizzaStore_ directory.
@@ -88,47 +88,54 @@ Congratulations! You've created an API by using a minimal API template.
88
88
89
89
Use Swagger to ensure that you have a self-documenting API, where the docs change when you change the code. This also builds a really convenient web interface for your API, so you can test out the application as you build it.
90
90
91
-
1. Install the *Swashbuckle* package:
92
-
93
-
```bash
94
-
dotnet add package Swashbuckle.AspNetCore
95
-
```
96
-
97
-
1. Next, configure your project to use Swagger. Open _Program.cs_ and add replace it with the following code:
91
+
1. In .NET 9, Swagger support is built in for minimal APIs! Update your _Program.cs_ file with the following code:
98
92
99
93
```csharp
100
94
usingMicrosoft.OpenApi.Models;
101
95
102
96
varbuilder=WebApplication.CreateBuilder(args);
103
-
97
+
98
+
// Add services to the container
99
+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
104
100
builder.Services.AddEndpointsApiExplorer();
105
-
builder.Services.AddSwaggerGen(c =>
101
+
builder.Services.AddSwaggerGen(options=>
106
102
{
107
-
c.SwaggerDoc("v1", new OpenApiInfo { Title = "PizzaStore API", Description = "Making the Pizzas you love", Version = "v1" });
operation.Parameters[0].Description="The unique identifier for the pizza to delete";
267
+
returnoperation;
268
+
});
225
269
```
226
270
227
-
This is the actual API part of the application! As you can see, there's not a lot of code. We're just mapping the routes to code that calls into our in-memory store.
271
+
This is the actual API part of the application! In .NET 9, we're improving the OpenAPI documentation by:
272
+
273
+
- Using `.WithTags()` to organize endpoints in the Swagger UI
274
+
- Adding `.WithSummary()` and `.WithDescription()` to provide clear documentation
275
+
- Using the advanced `.WithOpenApi()` overload to customize parameter descriptions
276
+
- Organizing routes with `MapGroup()` for cleaner code
228
277
229
278
1. Run the app by using `dotnet run`:
230
279
231
280
```bash
232
281
dotnet run
233
282
```
234
283
235
-
1. In your browser, go to `http://localhost:{PORT}/swagger`.
236
-
237
-
You should see the following page rendering:
284
+
1. In your browser, go to `http://localhost:{PORT}/swagger`. You should see the following page rendering:
238
285
239
286

240
287
241
-
What's great about this Swagger UI is that you can use it to test out the API. Click on any of the API endpoints to expand it, and click the `Try it out` button. You'll see a form that makes it easy to try submitting requests to the API and seeing the response.
288
+
What's great about this Swagger UI is that you can:
289
+
290
+
- Browse all available endpoints organized by tags
291
+
- See detailed documentation including summaries and descriptions
292
+
- Expand any endpoint to see request parameters and response types
293
+
- Try out the API directly with the "Try it out" button
294
+
- Execute requests and see the actual responses without leaving the browser
242
295
243
296
## What's next?
244
297
245
-
This is a quick first look at building a backend with Minimal APIs. To go through this same example inmore detail, and with more explanation, check out the [Minimal API learning path on Microsoft Learn](https://docs.microsoft.com/learn/paths/aspnet-core-minimal-api/)!
298
+
This is a quick first look at building a backend with Minimal APIs in .NET 9. To learn more about the latest features in Minimal APIs and OpenAPI support, check out the [.NET 9 minimal API documentation](https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis) and [OpenAPI documents in ASP.NET Core](https://learn.microsoft.com/aspnet/core/fundamentals/openapi/using-openapi-documents).
246
299
247
300
In the next lesson, you'll learn about building a game with Blazor! Stay tuned!
0 commit comments