diff --git a/4-minimal-api/0-start/PizzaStore/Properties/launchSettings.json b/4-minimal-api/0-start/PizzaStore/Properties/launchSettings.json
index 36c68b3..7c30418 100644
--- a/4-minimal-api/0-start/PizzaStore/Properties/launchSettings.json
+++ b/4-minimal-api/0-start/PizzaStore/Properties/launchSettings.json
@@ -1,18 +1,11 @@
{
- "iisSettings": {
- "windowsAuthentication": false,
- "anonymousAuthentication": true,
- "iisExpress": {
- "applicationUrl": "http://localhost:1201",
- "sslPort": 44373
- }
- },
+ "$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
- "applicationUrl": "http://localhost:5112",
+ "applicationUrl": "http://localhost:5019",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
@@ -21,14 +14,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
- "applicationUrl": "https://localhost:7192;http://localhost:5112",
- "environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development"
- }
- },
- "IIS Express": {
- "commandName": "IISExpress",
- "launchBrowser": true,
+ "applicationUrl": "https://localhost:7084;http://localhost:5019",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
diff --git a/4-minimal-api/1-complete/PizzaStore/PizzaStore.csproj b/4-minimal-api/1-complete/PizzaStore/PizzaStore.csproj
index 5e63f79..cb5a23c 100644
--- a/4-minimal-api/1-complete/PizzaStore/PizzaStore.csproj
+++ b/4-minimal-api/1-complete/PizzaStore/PizzaStore.csproj
@@ -7,7 +7,8 @@
-
+
+
diff --git a/4-minimal-api/1-complete/PizzaStore/Program.cs b/4-minimal-api/1-complete/PizzaStore/Program.cs
index 405992d..4d72ae6 100644
--- a/4-minimal-api/1-complete/PizzaStore/Program.cs
+++ b/4-minimal-api/1-complete/PizzaStore/Program.cs
@@ -1,32 +1,82 @@
-using PizzaStore.DB;
using Microsoft.OpenApi.Models;
+using PizzaStore.DB;
var builder = WebApplication.CreateBuilder(args);
-
+
+// Add services to the container
+// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
-builder.Services.AddSwaggerGen(c =>
+builder.Services.AddSwaggerGen(options =>
{
- c.SwaggerDoc("v1", new OpenApiInfo { Title = "PizzaStore API", Description = "Making the Pizzas you love", Version = "v1" });
+ options.SwaggerDoc("v1", new OpenApiInfo
+ {
+ Title = "PizzaStore API",
+ Description = "Making the Pizzas you love",
+ Version = "v1",
+ Contact = new OpenApiContact
+ {
+ Name = "Pizza Support",
+ Email = "pizza@example.com"
+ }
+ });
});
-
+
var app = builder.Build();
-
+
+// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
- app.UseDeveloperExceptionPage();
- app.UseSwagger();
- app.UseSwaggerUI(c => // UseSwaggerUI Protected by if (env.IsDevelopment())
- {
- c.SwaggerEndpoint("/swagger/v1/swagger.json", "PizzaStore API V1");
- });
+ app.UseSwagger();
+ app.UseSwaggerUI(options =>
+ {
+ options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
+ options.RoutePrefix = "swagger";
+ });
}
-
-app.MapGet("/", () => "Hello World!");
-
-app.MapGet("/pizzas/{id}", (int id) => PizzaDB.GetPizza(id));
-app.MapGet("/pizzas", () => PizzaDB.GetPizzas());
-app.MapPost("/pizzas", (Pizza pizza) => PizzaDB.CreatePizza(pizza));
-app.MapPut("/pizzas", (Pizza pizza) => PizzaDB.UpdatePizza(pizza));
-app.MapDelete("/pizzas/{id}", (int id) => PizzaDB.RemovePizza(id));
-app.Run();
+app.MapGet("/", () => "Hello World!");
+
+// Define API endpoints with OpenAPI descriptions
+var pizzas = app.MapGroup("/pizzas")
+ .WithTags("Pizzas")
+ .WithOpenApi();
+
+// Get all pizzas
+pizzas.MapGet("/", () => PizzaDB.GetPizzas())
+ .WithName("GetAllPizzas")
+ .WithSummary("Get all pizzas")
+ .WithDescription("Retrieves the complete list of available pizzas");
+
+// Get pizza by ID
+pizzas.MapGet("/{id}", (int id) => PizzaDB.GetPizza(id))
+ .WithName("GetPizzaById")
+ .WithSummary("Get pizza by ID")
+ .WithDescription("Gets a specific pizza by its unique identifier")
+ .WithOpenApi(operation => {
+ operation.Parameters[0].Description = "The unique identifier for the pizza";
+ return operation;
+ });
+
+// Create a new pizza
+pizzas.MapPost("/", (Pizza pizza) => PizzaDB.CreatePizza(pizza))
+ .WithName("CreatePizza")
+ .WithSummary("Create a new pizza")
+ .WithDescription("Adds a new pizza to the menu");
+
+// Update a pizza
+pizzas.MapPut("/", (Pizza pizza) => PizzaDB.UpdatePizza(pizza))
+ .WithName("UpdatePizza")
+ .WithSummary("Update an existing pizza")
+ .WithDescription("Updates the details of an existing pizza");
+
+// Delete a pizza
+pizzas.MapDelete("/{id}", (int id) => PizzaDB.RemovePizza(id))
+ .WithName("DeletePizza")
+ .WithSummary("Delete a pizza")
+ .WithDescription("Removes a pizza from the menu")
+ .WithOpenApi(operation => {
+ operation.Parameters[0].Description = "The unique identifier for the pizza to delete";
+ return operation;
+ });
+
+app.Run();
\ No newline at end of file
diff --git a/4-minimal-api/1-complete/PizzaStore/Properties/launchSettings.json b/4-minimal-api/1-complete/PizzaStore/Properties/launchSettings.json
index 36c68b3..7c30418 100644
--- a/4-minimal-api/1-complete/PizzaStore/Properties/launchSettings.json
+++ b/4-minimal-api/1-complete/PizzaStore/Properties/launchSettings.json
@@ -1,18 +1,11 @@
{
- "iisSettings": {
- "windowsAuthentication": false,
- "anonymousAuthentication": true,
- "iisExpress": {
- "applicationUrl": "http://localhost:1201",
- "sslPort": 44373
- }
- },
+ "$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
- "applicationUrl": "http://localhost:5112",
+ "applicationUrl": "http://localhost:5019",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
@@ -21,14 +14,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
- "applicationUrl": "https://localhost:7192;http://localhost:5112",
- "environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development"
- }
- },
- "IIS Express": {
- "commandName": "IISExpress",
- "launchBrowser": true,
+ "applicationUrl": "https://localhost:7084;http://localhost:5019",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
diff --git a/4-minimal-api/README.md b/4-minimal-api/README.md
index 464e0be..daa3e37 100644
--- a/4-minimal-api/README.md
+++ b/4-minimal-api/README.md
@@ -88,7 +88,15 @@ Congratulations! You've created an API by using a minimal API template.
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.
-1. In .NET 9, Swagger support is built in for minimal APIs! Update your _Program.cs_ file with the following code:
+1. First, add the required Swagger packages to your project:
+
+ ```bash
+ cd PizzaStore
+ dotnet add package Swashbuckle.AspNetCore
+ dotnet add package Microsoft.AspNetCore.OpenApi
+ ```
+
+2. Now update your _Program.cs_ file with the following code:
```csharp
using Microsoft.OpenApi.Models;
@@ -104,12 +112,7 @@ Use Swagger to ensure that you have a self-documenting API, where the docs chang
{
Title = "PizzaStore API",
Description = "Making the Pizzas you love",
- Version = "v1",
- Contact = new OpenApiContact
- {
- Name = "Pizza Support",
- Email = "pizza@example.com"
- }
+ Version = "v1"
});
});
@@ -119,11 +122,7 @@ Use Swagger to ensure that you have a self-documenting API, where the docs chang
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
- app.UseSwaggerUI(options =>
- {
- options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
- options.RoutePrefix = "swagger";
- });
+ app.UseSwaggerUI();
}
app.MapGet("/", () => "Hello World!");
@@ -137,6 +136,10 @@ Use Swagger to ensure that you have a self-documenting API, where the docs chang
- Adds the `AddSwaggerGen` service to generate the OpenAPI specification for your API
- Configures Swagger UI which provides an interactive UI for testing your API endpoints
+ - Adds the `AddEndpointsApiExplorer` service which is required for Swagger to discover and generate documentation for your API endpoints
+ - Adds the `AddSwaggerGen` service to generate the OpenAPI specification for your API
+ - Configures Swagger UI which provides an interactive UI for testing your API endpoints
+
1. Rerun the project and go to the app's address, `http://localhost:{PORT}/swagger`.
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
### Add a Pizza model and service
-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.
+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.
-1. Create the file _Db.cs_ and give it the following content:
+1. Create the file _Db.cs_ in your project directory and give it the following content:
```csharp
+ using System.Collections.Generic;
+ using System.Linq;
+
namespace PizzaStore.DB;
public record Pizza
{
- public int Id {get; set;}
- public string ? Name { get; set; }
+ public int Id { get; set; }
+ public string? Name { get; set; }
}
- public class PizzaDB
+ public static class PizzaDB
{
private static List _pizzas = new List()
{
@@ -172,7 +178,7 @@ First you need some data. To store and manage data, you'll use an in-memory stor
return _pizzas;
}
- public static Pizza ? GetPizza(int id)
+ public static Pizza? GetPizza(int id)
{
return _pizzas.SingleOrDefault(pizza => pizza.Id == id);
}
@@ -215,9 +221,10 @@ To connect your in-memory store to the API:
Now, connect data in your API.
-1. At the top of the _Program.cs_ file, add the following line of code:
+1. At the top of the _Program.cs_ file, add the following line of code alongside the existing using statement:
```csharp
+ using Microsoft.OpenApi.Models;
using PizzaStore.DB;
```