Skip to content

Commit 683f1af

Browse files
committed
Update launchSettings.json, Program.cs, and README.md for .NET 9 compatibility and enhanced Swagger integration
1 parent b8955df commit 683f1af

File tree

5 files changed

+105
-75
lines changed

5 files changed

+105
-75
lines changed

4-minimal-api/0-start/PizzaStore/Properties/launchSettings.json

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
{
2-
"iisSettings": {
3-
"windowsAuthentication": false,
4-
"anonymousAuthentication": true,
5-
"iisExpress": {
6-
"applicationUrl": "http://localhost:1201",
7-
"sslPort": 44373
8-
}
9-
},
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
103
"profiles": {
114
"http": {
125
"commandName": "Project",
136
"dotnetRunMessages": true,
147
"launchBrowser": true,
15-
"applicationUrl": "http://localhost:5112",
8+
"applicationUrl": "http://localhost:5019",
169
"environmentVariables": {
1710
"ASPNETCORE_ENVIRONMENT": "Development"
1811
}
@@ -21,14 +14,7 @@
2114
"commandName": "Project",
2215
"dotnetRunMessages": true,
2316
"launchBrowser": true,
24-
"applicationUrl": "https://localhost:7192;http://localhost:5112",
25-
"environmentVariables": {
26-
"ASPNETCORE_ENVIRONMENT": "Development"
27-
}
28-
},
29-
"IIS Express": {
30-
"commandName": "IISExpress",
31-
"launchBrowser": true,
17+
"applicationUrl": "https://localhost:7084;http://localhost:5019",
3218
"environmentVariables": {
3319
"ASPNETCORE_ENVIRONMENT": "Development"
3420
}

4-minimal-api/1-complete/PizzaStore/PizzaStore.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.4" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.1" />
1112
</ItemGroup>
1213

1314
</Project>
Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,82 @@
1-
using PizzaStore.DB;
21
using Microsoft.OpenApi.Models;
2+
using PizzaStore.DB;
33

44
var builder = WebApplication.CreateBuilder(args);
5-
5+
6+
// Add services to the container
7+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
68
builder.Services.AddEndpointsApiExplorer();
7-
builder.Services.AddSwaggerGen(c =>
9+
builder.Services.AddSwaggerGen(options =>
810
{
9-
c.SwaggerDoc("v1", new OpenApiInfo { Title = "PizzaStore API", Description = "Making the Pizzas you love", Version = "v1" });
11+
options.SwaggerDoc("v1", new OpenApiInfo
12+
{
13+
Title = "PizzaStore API",
14+
Description = "Making the Pizzas you love",
15+
Version = "v1",
16+
Contact = new OpenApiContact
17+
{
18+
Name = "Pizza Support",
19+
20+
}
21+
});
1022
});
11-
23+
1224
var app = builder.Build();
13-
25+
26+
// Configure the HTTP request pipeline
1427
if (app.Environment.IsDevelopment())
1528
{
16-
app.UseDeveloperExceptionPage();
17-
app.UseSwagger();
18-
app.UseSwaggerUI(c => // UseSwaggerUI Protected by if (env.IsDevelopment())
19-
{
20-
c.SwaggerEndpoint("/swagger/v1/swagger.json", "PizzaStore API V1");
21-
});
29+
app.UseSwagger();
30+
app.UseSwaggerUI(options =>
31+
{
32+
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
33+
options.RoutePrefix = "swagger";
34+
});
2235
}
23-
36+
2437
app.MapGet("/", () => "Hello World!");
25-
26-
app.MapGet("/pizzas/{id}", (int id) => PizzaDB.GetPizza(id));
27-
app.MapGet("/pizzas", () => PizzaDB.GetPizzas());
28-
app.MapPost("/pizzas", (Pizza pizza) => PizzaDB.CreatePizza(pizza));
29-
app.MapPut("/pizzas", (Pizza pizza) => PizzaDB.UpdatePizza(pizza));
30-
app.MapDelete("/pizzas/{id}", (int id) => PizzaDB.RemovePizza(id));
31-
32-
app.Run();
38+
39+
// Define API endpoints with OpenAPI descriptions
40+
var pizzas = app.MapGroup("/pizzas")
41+
.WithTags("Pizzas")
42+
.WithOpenApi();
43+
44+
// Get all pizzas
45+
pizzas.MapGet("/", () => PizzaDB.GetPizzas())
46+
.WithName("GetAllPizzas")
47+
.WithSummary("Get all pizzas")
48+
.WithDescription("Retrieves the complete list of available pizzas");
49+
50+
// Get pizza by ID
51+
pizzas.MapGet("/{id}", (int id) => PizzaDB.GetPizza(id))
52+
.WithName("GetPizzaById")
53+
.WithSummary("Get pizza by ID")
54+
.WithDescription("Gets a specific pizza by its unique identifier")
55+
.WithOpenApi(operation => {
56+
operation.Parameters[0].Description = "The unique identifier for the pizza";
57+
return operation;
58+
});
59+
60+
// Create a new pizza
61+
pizzas.MapPost("/", (Pizza pizza) => PizzaDB.CreatePizza(pizza))
62+
.WithName("CreatePizza")
63+
.WithSummary("Create a new pizza")
64+
.WithDescription("Adds a new pizza to the menu");
65+
66+
// Update a pizza
67+
pizzas.MapPut("/", (Pizza pizza) => PizzaDB.UpdatePizza(pizza))
68+
.WithName("UpdatePizza")
69+
.WithSummary("Update an existing pizza")
70+
.WithDescription("Updates the details of an existing pizza");
71+
72+
// Delete a pizza
73+
pizzas.MapDelete("/{id}", (int id) => PizzaDB.RemovePizza(id))
74+
.WithName("DeletePizza")
75+
.WithSummary("Delete a pizza")
76+
.WithDescription("Removes a pizza from the menu")
77+
.WithOpenApi(operation => {
78+
operation.Parameters[0].Description = "The unique identifier for the pizza to delete";
79+
return operation;
80+
});
81+
82+
app.Run();

4-minimal-api/1-complete/PizzaStore/Properties/launchSettings.json

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
{
2-
"iisSettings": {
3-
"windowsAuthentication": false,
4-
"anonymousAuthentication": true,
5-
"iisExpress": {
6-
"applicationUrl": "http://localhost:1201",
7-
"sslPort": 44373
8-
}
9-
},
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
103
"profiles": {
114
"http": {
125
"commandName": "Project",
136
"dotnetRunMessages": true,
147
"launchBrowser": true,
15-
"applicationUrl": "http://localhost:5112",
8+
"applicationUrl": "http://localhost:5019",
169
"environmentVariables": {
1710
"ASPNETCORE_ENVIRONMENT": "Development"
1811
}
@@ -21,14 +14,7 @@
2114
"commandName": "Project",
2215
"dotnetRunMessages": true,
2316
"launchBrowser": true,
24-
"applicationUrl": "https://localhost:7192;http://localhost:5112",
25-
"environmentVariables": {
26-
"ASPNETCORE_ENVIRONMENT": "Development"
27-
}
28-
},
29-
"IIS Express": {
30-
"commandName": "IISExpress",
31-
"launchBrowser": true,
17+
"applicationUrl": "https://localhost:7084;http://localhost:5019",
3218
"environmentVariables": {
3319
"ASPNETCORE_ENVIRONMENT": "Development"
3420
}

4-minimal-api/README.md

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,15 @@ Congratulations! You've created an API by using a minimal API template.
8888

8989
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.
9090

91-
1. In .NET 9, Swagger support is built in for minimal APIs! Update your _Program.cs_ file with the following code:
91+
1. First, add the required Swagger packages to your project:
92+
93+
```bash
94+
cd PizzaStore
95+
dotnet add package Swashbuckle.AspNetCore
96+
dotnet add package Microsoft.AspNetCore.OpenApi
97+
```
98+
99+
2. Now update your _Program.cs_ file with the following code:
92100

93101
```csharp
94102
using Microsoft.OpenApi.Models;
@@ -104,12 +112,7 @@ Use Swagger to ensure that you have a self-documenting API, where the docs chang
104112
{
105113
Title = "PizzaStore API",
106114
Description = "Making the Pizzas you love",
107-
Version = "v1",
108-
Contact = new OpenApiContact
109-
{
110-
Name = "Pizza Support",
111-
112-
}
115+
Version = "v1"
113116
});
114117
});
115118
@@ -119,11 +122,7 @@ Use Swagger to ensure that you have a self-documenting API, where the docs chang
119122
if (app.Environment.IsDevelopment())
120123
{
121124
app.UseSwagger();
122-
app.UseSwaggerUI(options =>
123-
{
124-
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
125-
options.RoutePrefix = "swagger";
126-
});
125+
app.UseSwaggerUI();
127126
}
128127
129128
app.MapGet("/", () => "Hello World!");
@@ -137,6 +136,10 @@ Use Swagger to ensure that you have a self-documenting API, where the docs chang
137136
- Adds the `AddSwaggerGen` service to generate the OpenAPI specification for your API
138137
- Configures Swagger UI which provides an interactive UI for testing your API endpoints
139138
139+
- Adds the `AddEndpointsApiExplorer` service which is required for Swagger to discover and generate documentation for your API endpoints
140+
- Adds the `AddSwaggerGen` service to generate the OpenAPI specification for your API
141+
- Configures Swagger UI which provides an interactive UI for testing your API endpoints
142+
140143
1. Rerun the project and go to the app's address, `http://localhost:{PORT}/swagger`.
141144
142145
You should see the following output:
@@ -145,20 +148,23 @@ Use Swagger to ensure that you have a self-documenting API, where the docs chang
145148
146149
### Add a Pizza model and service
147150
148-
First you need some data. To store and manage data, you'll use an in-memory store. For this example, we're just going to use a simple list of pizzas. Don't worry too much about this pizza service, it's just a quick example that holds a list of pizzas so we our API has some data to work with.
151+
First you need some data. To store and manage data, you'll use an in-memory store. For this example, we're just going to use a simple list of pizzas.
149152
150-
1. Create the file _Db.cs_ and give it the following content:
153+
1. Create the file _Db.cs_ in your project directory and give it the following content:
151154
152155
```csharp
156+
using System.Collections.Generic;
157+
using System.Linq;
158+
153159
namespace PizzaStore.DB;
154160
155161
public record Pizza
156162
{
157-
public int Id {get; set;}
158-
public string ? Name { get; set; }
163+
public int Id { get; set; }
164+
public string? Name { get; set; }
159165
}
160166
161-
public class PizzaDB
167+
public static class PizzaDB
162168
{
163169
private static List<Pizza> _pizzas = new List<Pizza>()
164170
{
@@ -172,7 +178,7 @@ First you need some data. To store and manage data, you'll use an in-memory stor
172178
return _pizzas;
173179
}
174180
175-
public static Pizza ? GetPizza(int id)
181+
public static Pizza? GetPizza(int id)
176182
{
177183
return _pizzas.SingleOrDefault(pizza => pizza.Id == id);
178184
}
@@ -215,9 +221,10 @@ To connect your in-memory store to the API:
215221
216222
Now, connect data in your API.
217223
218-
1. At the top of the _Program.cs_ file, add the following line of code:
224+
1. At the top of the _Program.cs_ file, add the following line of code alongside the existing using statement:
219225
220226
```csharp
227+
using Microsoft.OpenApi.Models;
221228
using PizzaStore.DB;
222229
```
223230

0 commit comments

Comments
 (0)