Skip to content

Commit 1ea99d6

Browse files
authored
Update Minimal API tutorial for .NET 7 (#27070)
1 parent d351710 commit 1ea99d6

File tree

23 files changed

+612
-198
lines changed

23 files changed

+612
-198
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [Visual Studio 2022 for Mac latest preview](https://visualstudio.microsoft.com/vs/mac/preview/)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [Visual Studio 2022 latest preview version](https://visualstudio.microsoft.com/vs/#download) with the **ASP.NET and web development** workload.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
* [Visual Studio Code](https://code.visualstudio.com/download)
2+
* [C# for Visual Studio Code (latest version)](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp)
3+
* [!INCLUDE [.NET 7.0 SDK](~/includes/7.0-SDK.md)]
4+
5+
The Visual Studio Code instructions use the .NET CLI for ASP.NET Core development functions such as project creation. You can follow these instructions on macOS, Linux, or Windows and with any code editor. Minor changes may be required if you use something other than Visual Studio Code.

aspnetcore/tutorials/min-web-api.md

Lines changed: 184 additions & 198 deletions
Large diffs are not rendered by default.
Loading
Loading
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#define FINAL // MINIMAL FINAL
2+
#if MINIMAL
3+
// <snippet_min>
4+
var builder = WebApplication.CreateBuilder(args);
5+
var app = builder.Build();
6+
7+
app.MapGet("/", () => "Hello World!");
8+
9+
app.Run();
10+
// </snippet_min>
11+
#elif FINAL
12+
// <snippet_all>
13+
using Microsoft.EntityFrameworkCore;
14+
15+
// <snippet_DI>
16+
var builder = WebApplication.CreateBuilder(args);
17+
builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoList"));
18+
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
19+
var app = builder.Build();
20+
// </snippet_DI>
21+
22+
// <snippet_get>
23+
app.MapGet("/todoitems", async (TodoDb db) =>
24+
await db.Todos.ToListAsync());
25+
26+
app.MapGet("/todoitems/complete", async (TodoDb db) =>
27+
await db.Todos.Where(t => t.IsComplete).ToListAsync());
28+
29+
// <snippet_getCustom>
30+
app.MapGet("/todoitems/{id}", async (int id, TodoDb db) =>
31+
await db.Todos.FindAsync(id)
32+
is Todo todo
33+
? Results.Ok(todo)
34+
: Results.NotFound());
35+
// </snippet_getCustom>
36+
// </snippet_get>
37+
38+
// <snippet_post>
39+
app.MapPost("/todoitems", async (Todo todo, TodoDb db) =>
40+
{
41+
db.Todos.Add(todo);
42+
await db.SaveChangesAsync();
43+
44+
return Results.Created($"/todoitems/{todo.Id}", todo);
45+
});
46+
// </snippet_post>
47+
48+
// <snippet_put>
49+
app.MapPut("/todoitems/{id}", async (int id, Todo inputTodo, TodoDb db) =>
50+
{
51+
var todo = await db.Todos.FindAsync(id);
52+
53+
if (todo is null) return Results.NotFound();
54+
55+
todo.Name = inputTodo.Name;
56+
todo.IsComplete = inputTodo.IsComplete;
57+
58+
await db.SaveChangesAsync();
59+
60+
return Results.NoContent();
61+
});
62+
// </snippet_put>
63+
64+
// <snippet_delete>
65+
app.MapDelete("/todoitems/{id}", async (int id, TodoDb db) =>
66+
{
67+
if (await db.Todos.FindAsync(id) is Todo todo)
68+
{
69+
db.Todos.Remove(todo);
70+
await db.SaveChangesAsync();
71+
return Results.Ok(todo);
72+
}
73+
74+
return Results.NotFound();
75+
});
76+
// </snippet_delete>
77+
78+
app.Run();
79+
// </snippet_all>
80+
#endif
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Todo
2+
{
3+
public int Id { get; set; }
4+
public string? Name { get; set; }
5+
public bool IsComplete { get; set; }
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
11+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.0-rc.1.22427.2" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.1.22426.7" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
class TodoDb : DbContext
4+
{
5+
public TodoDb(DbContextOptions<TodoDb> options)
6+
: base(options) { }
7+
8+
public DbSet<Todo> Todos => Set<Todo>();
9+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// <snippet_all>
2+
using Microsoft.EntityFrameworkCore;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoList"));
6+
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
7+
var app = builder.Build();
8+
9+
// <snippet_group>
10+
RouteGroupBuilder todoItems = app.MapGroup("/todoitems");
11+
12+
todoItems.MapGet("/", GetAllTodos);
13+
todoItems.MapGet("/{id}", GetTodo);
14+
todoItems.MapPost("/", CreateTodo);
15+
todoItems.MapPut("/{id}", UpdateTodo);
16+
todoItems.MapDelete("/{id}", DeleteTodo);
17+
// </snippet_group>
18+
19+
app.Run();
20+
21+
// <snippet_handlers>
22+
// <snippet_getalltodos>
23+
static async Task<IResult> GetAllTodos(TodoDb db)
24+
{
25+
return TypedResults.Ok(await db.Todos.Select(x => new TodoItemDTO(x)).ToArrayAsync());
26+
}
27+
// </snippet_getalltodos>
28+
29+
static async Task<IResult> GetTodo(int id, TodoDb db)
30+
{
31+
return await db.Todos.FindAsync(id)
32+
is Todo todo
33+
? TypedResults.Ok(new TodoItemDTO(todo))
34+
: TypedResults.NotFound();
35+
}
36+
37+
static async Task<IResult> CreateTodo(TodoItemDTO todoItemDTO, TodoDb db)
38+
{
39+
var todoItem = new Todo
40+
{
41+
IsComplete = todoItemDTO.IsComplete,
42+
Name = todoItemDTO.Name
43+
};
44+
45+
db.Todos.Add(todoItem);
46+
await db.SaveChangesAsync();
47+
48+
return TypedResults.Created($"/todoitems/{todoItem.Id}", todoItemDTO);
49+
}
50+
51+
static async Task<IResult> UpdateTodo(int id, TodoItemDTO todoItemDTO, TodoDb db)
52+
{
53+
var todo = await db.Todos.FindAsync(id);
54+
55+
if (todo is null) return TypedResults.NotFound();
56+
57+
todo.Name = todoItemDTO.Name;
58+
todo.IsComplete = todoItemDTO.IsComplete;
59+
60+
await db.SaveChangesAsync();
61+
62+
return TypedResults.NoContent();
63+
}
64+
65+
static async Task<IResult> DeleteTodo(int id, TodoDb db)
66+
{
67+
if (await db.Todos.FindAsync(id) is Todo todo)
68+
{
69+
db.Todos.Remove(todo);
70+
await db.SaveChangesAsync();
71+
return TypedResults.Ok(todo);
72+
}
73+
74+
return TypedResults.NotFound();
75+
}
76+
// <snippet_handlers>
77+
// </snippet_all>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Todo
2+
{
3+
public int Id { get; set; }
4+
public string? Name { get; set; }
5+
public bool IsComplete { get; set; }
6+
public string? Secret { get; set; }
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
11+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.0-rc.1.22427.2" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.1.22426.7" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
class TodoDb : DbContext
4+
{
5+
public TodoDb(DbContextOptions<TodoDb> options)
6+
: base(options) { }
7+
8+
public DbSet<Todo> Todos => Set<Todo>();
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class TodoItemDTO
2+
{
3+
public int Id { get; set; }
4+
public string? Name { get; set; }
5+
public bool IsComplete { get; set; }
6+
7+
public TodoItemDTO() { }
8+
public TodoItemDTO(Todo todoItem) =>
9+
(Id, Name, IsComplete) = (todoItem.Id, todoItem.Name, todoItem.IsComplete);
10+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// <snippet_all>
2+
using Microsoft.EntityFrameworkCore;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoList"));
6+
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
7+
var app = builder.Build();
8+
9+
// <snippet_group>
10+
var todoItems = app.MapGroup("/todoitems");
11+
12+
todoItems.MapGet("/", async (TodoDb db) =>
13+
await db.Todos.ToListAsync());
14+
15+
todoItems.MapGet("/complete", async (TodoDb db) =>
16+
await db.Todos.Where(t => t.IsComplete).ToListAsync());
17+
18+
todoItems.MapGet("/{id}", async (int id, TodoDb db) =>
19+
await db.Todos.FindAsync(id)
20+
is Todo todo
21+
? Results.Ok(todo)
22+
: Results.NotFound());
23+
24+
todoItems.MapPost("/", async (Todo todo, TodoDb db) =>
25+
{
26+
db.Todos.Add(todo);
27+
await db.SaveChangesAsync();
28+
29+
return Results.Created($"/todoitems/{todo.Id}", todo);
30+
});
31+
32+
todoItems.MapPut("/{id}", async (int id, Todo inputTodo, TodoDb db) =>
33+
{
34+
var todo = await db.Todos.FindAsync(id);
35+
36+
if (todo is null) return Results.NotFound();
37+
38+
todo.Name = inputTodo.Name;
39+
todo.IsComplete = inputTodo.IsComplete;
40+
41+
await db.SaveChangesAsync();
42+
43+
return Results.NoContent();
44+
});
45+
46+
todoItems.MapDelete("/{id}", async (int id, TodoDb db) =>
47+
{
48+
if (await db.Todos.FindAsync(id) is Todo todo)
49+
{
50+
db.Todos.Remove(todo);
51+
await db.SaveChangesAsync();
52+
return Results.Ok(todo);
53+
}
54+
55+
return Results.NotFound();
56+
});
57+
// </snippet_group>
58+
59+
app.Run();
60+
// </snippet_all>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Todo
2+
{
3+
public int Id { get; set; }
4+
public string? Name { get; set; }
5+
public bool IsComplete { get; set; }
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
11+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.0-rc.1.22427.2" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.1.22426.7" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
class TodoDb : DbContext
4+
{
5+
public TodoDb(DbContextOptions<TodoDb> options)
6+
: base(options) { }
7+
8+
public DbSet<Todo> Todos => Set<Todo>();
9+
}

0 commit comments

Comments
 (0)