mirror of
https://github.com/dotnet/intro-to-dotnet-web-dev.git
synced 2025-12-06 05:32:03 +01:00
Update launchSettings.json, Program.cs, and README.md for .NET 9 compatibility and enhanced Swagger integration
This commit is contained in:
parent
b8955df40b
commit
683f1af5b8
|
|
@ -1,18 +1,11 @@
|
||||||
{
|
{
|
||||||
"iisSettings": {
|
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||||
"windowsAuthentication": false,
|
|
||||||
"anonymousAuthentication": true,
|
|
||||||
"iisExpress": {
|
|
||||||
"applicationUrl": "http://localhost:1201",
|
|
||||||
"sslPort": 44373
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"profiles": {
|
"profiles": {
|
||||||
"http": {
|
"http": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"dotnetRunMessages": true,
|
"dotnetRunMessages": true,
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"applicationUrl": "http://localhost:5112",
|
"applicationUrl": "http://localhost:5019",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
}
|
}
|
||||||
|
|
@ -21,14 +14,7 @@
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"dotnetRunMessages": true,
|
"dotnetRunMessages": true,
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"applicationUrl": "https://localhost:7192;http://localhost:5112",
|
"applicationUrl": "https://localhost:7084;http://localhost:5019",
|
||||||
"environmentVariables": {
|
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"IIS Express": {
|
|
||||||
"commandName": "IISExpress",
|
|
||||||
"launchBrowser": true,
|
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,8 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.4" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,82 @@
|
||||||
using PizzaStore.DB;
|
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
|
using PizzaStore.DB;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
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.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();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Configure the HTTP request pipeline
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseDeveloperExceptionPage();
|
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
app.UseSwaggerUI(c => // UseSwaggerUI Protected by if (env.IsDevelopment())
|
app.UseSwaggerUI(options =>
|
||||||
{
|
{
|
||||||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "PizzaStore API V1");
|
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
|
||||||
|
options.RoutePrefix = "swagger";
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
app.MapGet("/", () => "Hello World!");
|
app.MapGet("/", () => "Hello World!");
|
||||||
|
|
||||||
app.MapGet("/pizzas/{id}", (int id) => PizzaDB.GetPizza(id));
|
// Define API endpoints with OpenAPI descriptions
|
||||||
app.MapGet("/pizzas", () => PizzaDB.GetPizzas());
|
var pizzas = app.MapGroup("/pizzas")
|
||||||
app.MapPost("/pizzas", (Pizza pizza) => PizzaDB.CreatePizza(pizza));
|
.WithTags("Pizzas")
|
||||||
app.MapPut("/pizzas", (Pizza pizza) => PizzaDB.UpdatePizza(pizza));
|
.WithOpenApi();
|
||||||
app.MapDelete("/pizzas/{id}", (int id) => PizzaDB.RemovePizza(id));
|
|
||||||
|
// 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();
|
app.Run();
|
||||||
|
|
@ -1,18 +1,11 @@
|
||||||
{
|
{
|
||||||
"iisSettings": {
|
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||||
"windowsAuthentication": false,
|
|
||||||
"anonymousAuthentication": true,
|
|
||||||
"iisExpress": {
|
|
||||||
"applicationUrl": "http://localhost:1201",
|
|
||||||
"sslPort": 44373
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"profiles": {
|
"profiles": {
|
||||||
"http": {
|
"http": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"dotnetRunMessages": true,
|
"dotnetRunMessages": true,
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"applicationUrl": "http://localhost:5112",
|
"applicationUrl": "http://localhost:5019",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
}
|
}
|
||||||
|
|
@ -21,14 +14,7 @@
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"dotnetRunMessages": true,
|
"dotnetRunMessages": true,
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"applicationUrl": "https://localhost:7192;http://localhost:5112",
|
"applicationUrl": "https://localhost:7084;http://localhost:5019",
|
||||||
"environmentVariables": {
|
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"IIS Express": {
|
|
||||||
"commandName": "IISExpress",
|
|
||||||
"launchBrowser": true,
|
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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.
|
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
|
```csharp
|
||||||
using Microsoft.OpenApi.Models;
|
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",
|
Title = "PizzaStore API",
|
||||||
Description = "Making the Pizzas you love",
|
Description = "Making the Pizzas you love",
|
||||||
Version = "v1",
|
Version = "v1"
|
||||||
Contact = new OpenApiContact
|
|
||||||
{
|
|
||||||
Name = "Pizza Support",
|
|
||||||
Email = "pizza@example.com"
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -119,11 +122,7 @@ Use Swagger to ensure that you have a self-documenting API, where the docs chang
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
app.UseSwaggerUI(options =>
|
app.UseSwaggerUI();
|
||||||
{
|
|
||||||
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
|
|
||||||
options.RoutePrefix = "swagger";
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.MapGet("/", () => "Hello World!");
|
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
|
- 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
|
- 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`.
|
1. Rerun the project and go to the app's address, `http://localhost:{PORT}/swagger`.
|
||||||
|
|
||||||
You should see the following output:
|
You should see the following output:
|
||||||
|
|
@ -145,11 +148,14 @@ Use Swagger to ensure that you have a self-documenting API, where the docs chang
|
||||||
|
|
||||||
### Add a Pizza model and service
|
### 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
|
```csharp
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace PizzaStore.DB;
|
namespace PizzaStore.DB;
|
||||||
|
|
||||||
public record Pizza
|
public record Pizza
|
||||||
|
|
@ -158,7 +164,7 @@ First you need some data. To store and manage data, you'll use an in-memory stor
|
||||||
public string? Name { get; set; }
|
public string? Name { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PizzaDB
|
public static class PizzaDB
|
||||||
{
|
{
|
||||||
private static List<Pizza> _pizzas = new List<Pizza>()
|
private static List<Pizza> _pizzas = new List<Pizza>()
|
||||||
{
|
{
|
||||||
|
|
@ -215,9 +221,10 @@ To connect your in-memory store to the API:
|
||||||
|
|
||||||
Now, connect data in your 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
|
```csharp
|
||||||
|
using Microsoft.OpenApi.Models;
|
||||||
using PizzaStore.DB;
|
using PizzaStore.DB;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue