Skip to content

Commit e05e297

Browse files
committed
update sample to target .NET 9.0
1 parent 9eba0f4 commit e05e297

File tree

7 files changed

+139
-144
lines changed

7 files changed

+139
-144
lines changed

aspnetcore/tutorials/first-web-api/samples/9.0/TodoApi/Controllers/TodoItemsController.cs

Lines changed: 79 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,108 +2,107 @@
22
using Microsoft.EntityFrameworkCore;
33
using TodoApi.Models;
44

5-
namespace TodoApi.Controllers
5+
namespace TodoApi.Controllers;
6+
7+
[Route("api/[controller]")]
8+
[ApiController]
9+
public class TodoItemsController : ControllerBase
610
{
7-
[Route("api/[controller]")]
8-
[ApiController]
9-
public class TodoItemsController : ControllerBase
11+
private readonly TodoContext _context;
12+
13+
public TodoItemsController(TodoContext context)
1014
{
11-
private readonly TodoContext _context;
15+
_context = context;
16+
}
1217

13-
public TodoItemsController(TodoContext context)
14-
{
15-
_context = context;
16-
}
18+
// GET: api/TodoItems
19+
[HttpGet]
20+
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
21+
{
22+
return await _context.TodoItems.ToListAsync();
23+
}
24+
25+
// GET: api/TodoItems/5
26+
// <snippet_GetByID>
27+
[HttpGet("{id}")]
28+
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
29+
{
30+
var todoItem = await _context.TodoItems.FindAsync(id);
1731

18-
// GET: api/TodoItems
19-
[HttpGet]
20-
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
32+
if (todoItem == null)
2133
{
22-
return await _context.TodoItems.ToListAsync();
34+
return NotFound();
2335
}
2436

25-
// GET: api/TodoItems/5
26-
// <snippet_GetByID>
27-
[HttpGet("{id}")]
28-
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
37+
return todoItem;
38+
}
39+
// </snippet_GetByID>
40+
41+
// PUT: api/TodoItems/5
42+
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
43+
// <snippet_Update>
44+
[HttpPut("{id}")]
45+
public async Task<IActionResult> PutTodoItem(long id, TodoItem todoItem)
46+
{
47+
if (id != todoItem.Id)
2948
{
30-
var todoItem = await _context.TodoItems.FindAsync(id);
49+
return BadRequest();
50+
}
3151

32-
if (todoItem == null)
33-
{
34-
return NotFound();
35-
}
52+
_context.Entry(todoItem).State = EntityState.Modified;
3653

37-
return todoItem;
54+
try
55+
{
56+
await _context.SaveChangesAsync();
3857
}
39-
// </snippet_GetByID>
40-
41-
// PUT: api/TodoItems/5
42-
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
43-
// <snippet_Update>
44-
[HttpPut("{id}")]
45-
public async Task<IActionResult> PutTodoItem(long id, TodoItem todoItem)
58+
catch (DbUpdateConcurrencyException)
4659
{
47-
if (id != todoItem.Id)
60+
if (!TodoItemExists(id))
4861
{
49-
return BadRequest();
50-
}
51-
52-
_context.Entry(todoItem).State = EntityState.Modified;
53-
54-
try
55-
{
56-
await _context.SaveChangesAsync();
62+
return NotFound();
5763
}
58-
catch (DbUpdateConcurrencyException)
64+
else
5965
{
60-
if (!TodoItemExists(id))
61-
{
62-
return NotFound();
63-
}
64-
else
65-
{
66-
throw;
67-
}
66+
throw;
6867
}
69-
70-
return NoContent();
7168
}
72-
// </snippet_Update>
7369

74-
// POST: api/TodoItems
75-
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
76-
// <snippet_Create>
77-
[HttpPost]
78-
public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
79-
{
80-
_context.TodoItems.Add(todoItem);
81-
await _context.SaveChangesAsync();
70+
return NoContent();
71+
}
72+
// </snippet_Update>
8273

83-
// return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem);
84-
return CreatedAtAction(nameof(GetTodoItem), new { id = todoItem.Id }, todoItem);
85-
}
86-
// </snippet_Create>
74+
// POST: api/TodoItems
75+
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
76+
// <snippet_Create>
77+
[HttpPost]
78+
public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
79+
{
80+
_context.TodoItems.Add(todoItem);
81+
await _context.SaveChangesAsync();
8782

88-
// DELETE: api/TodoItems/5
89-
[HttpDelete("{id}")]
90-
public async Task<IActionResult> DeleteTodoItem(long id)
83+
// return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem);
84+
return CreatedAtAction(nameof(GetTodoItem), new { id = todoItem.Id }, todoItem);
85+
}
86+
// </snippet_Create>
87+
88+
// DELETE: api/TodoItems/5
89+
[HttpDelete("{id}")]
90+
public async Task<IActionResult> DeleteTodoItem(long id)
91+
{
92+
var todoItem = await _context.TodoItems.FindAsync(id);
93+
if (todoItem == null)
9194
{
92-
var todoItem = await _context.TodoItems.FindAsync(id);
93-
if (todoItem == null)
94-
{
95-
return NotFound();
96-
}
95+
return NotFound();
96+
}
9797

98-
_context.TodoItems.Remove(todoItem);
99-
await _context.SaveChangesAsync();
98+
_context.TodoItems.Remove(todoItem);
99+
await _context.SaveChangesAsync();
100100

101-
return NoContent();
102-
}
101+
return NoContent();
102+
}
103103

104-
private bool TodoItemExists(long id)
105-
{
106-
return _context.TodoItems.Any(e => e.Id == id);
107-
}
104+
private bool TodoItemExists(long id)
105+
{
106+
return _context.TodoItems.Any(e => e.Id == id);
108107
}
109-
}
108+
}
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
using Microsoft.AspNetCore.Mvc;
22

3-
namespace TodoApi.Controllers
3+
namespace TodoApi.Controllers;
4+
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class WeatherForecastController : ControllerBase
48
{
5-
[ApiController]
6-
[Route("[controller]")]
7-
public class WeatherForecastController : ControllerBase
9+
private static readonly string[] Summaries = new[]
810
{
9-
private static readonly string[] Summaries = new[]
10-
{
11-
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
12-
};
11+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
12+
};
1313

14-
private readonly ILogger<WeatherForecastController> _logger;
14+
private readonly ILogger<WeatherForecastController> _logger;
1515

16-
public WeatherForecastController(ILogger<WeatherForecastController> logger)
17-
{
18-
_logger = logger;
19-
}
16+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
17+
{
18+
_logger = logger;
19+
}
2020

21-
[HttpGet(Name = "GetWeatherForecast")]
22-
public IEnumerable<WeatherForecast> Get()
23-
{
24-
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
21+
[HttpGet(Name = "GetWeatherForecast")]
22+
public IEnumerable<WeatherForecast> Get()
23+
{
24+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
2525
{
2626
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
2727
TemperatureC = Random.Shared.Next(-20, 55),
2828
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
2929
})
3030
.ToArray();
31-
}
3231
}
33-
}
32+
}

aspnetcore/tutorials/first-web-api/samples/9.0/TodoApi/TodoApi.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

99
<ItemGroup>
1010

11-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-rc.2.22476.2" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.2.22472.11" />
13-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.2.22472.11" />
14-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0-rc.2.22472.11">
11+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>
18-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.0-rc.2.22510.1" />
18+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="9.0.0" />
1919
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
2020
</ItemGroup>
2121

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
namespace TodoApi
1+
namespace TodoApi;
2+
3+
public class WeatherForecast
24
{
3-
public class WeatherForecast
4-
{
5-
public DateOnly Date { get; set; }
5+
public DateOnly Date { get; set; }
66

7-
public int TemperatureC { get; set; }
7+
public int TemperatureC { get; set; }
88

9-
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
1010

11-
public string? Summary { get; set; }
12-
}
13-
}
11+
public string? Summary { get; set; }
12+
}
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
using Microsoft.AspNetCore.Mvc;
22

3-
namespace TodoApi.Controllers
3+
namespace TodoApi.Controllers;
4+
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class WeatherForecastController : ControllerBase
48
{
5-
[ApiController]
6-
[Route("[controller]")]
7-
public class WeatherForecastController : ControllerBase
9+
private static readonly string[] Summaries = new[]
810
{
9-
private static readonly string[] Summaries = new[]
10-
{
1111
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
1212
};
1313

14-
private readonly ILogger<WeatherForecastController> _logger;
14+
private readonly ILogger<WeatherForecastController> _logger;
1515

16-
public WeatherForecastController(ILogger<WeatherForecastController> logger)
17-
{
18-
_logger = logger;
19-
}
16+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
17+
{
18+
_logger = logger;
19+
}
2020

21-
[HttpGet(Name = "GetWeatherForecast")]
22-
public IEnumerable<WeatherForecast> Get()
23-
{
24-
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
21+
[HttpGet(Name = "GetWeatherForecast")]
22+
public IEnumerable<WeatherForecast> Get()
23+
{
24+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
2525
{
2626
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
2727
TemperatureC = Random.Shared.Next(-20, 55),
2828
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
2929
})
3030
.ToArray();
31-
}
3231
}
3332
}

aspnetcore/tutorials/first-web-api/samples/9.0/TodoApiDTO/TodoApiDTO.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

99
<ItemGroup>
1010

11-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-rc.2.22476.2" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.2.22472.11" />
13-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.2.22472.11" />
14-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0-rc.2.22472.11">
11+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>
18-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.0-rc.2.22510.1" />
18+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="9.0.0" />
1919
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
2020
</ItemGroup>
2121

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
namespace TodoApi
1+
namespace TodoApi;
2+
3+
public class WeatherForecast
24
{
3-
public class WeatherForecast
4-
{
5-
public DateOnly Date { get; set; }
5+
public DateOnly Date { get; set; }
66

7-
public int TemperatureC { get; set; }
7+
public int TemperatureC { get; set; }
88

9-
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
1010

11-
public string? Summary { get; set; }
12-
}
11+
public string? Summary { get; set; }
1312
}

0 commit comments

Comments
 (0)