Skip to content

Commit 517d959

Browse files
timdeschryverwadepickettRick-Anderson
authored
update WebApi to .NET 9 (without Swagger) (#34746)
* Update .NET version references in first-web-api.md to 9.0 * update sample to target .NET 9.0 * rename example to _SwaggerExample * add OpenAPI example (no swagger) * update WebApi to .NET 9 (without swagger) * remove unused example * review fixes * apply changes from #34676 * include IsDevelopment codeblock to match the text description * VS new project settings moved to order they appear in dialog * Right-click or Command-click on project in VSC Control-click on a project in VSC on windows does not work, must be Right-click for Windows and Control-click on macOS for VSC. * Specifying the DTO file and location in the instruction * Apply suggestions from code review * Update author names in web API tutorial * Add comment block in first-web-api tutorial * Apply suggestions from code review revert * Update aspnetcore/tutorials/first-web-api.md * remove unused file 'create-new-project' --------- Co-authored-by: Wade Pickett <[email protected]> Co-authored-by: Rick Anderson <[email protected]>
1 parent ba8d1ea commit 517d959

27 files changed

+834
-229
lines changed

aspnetcore/tutorials/first-web-api.md

Lines changed: 413 additions & 73 deletions
Large diffs are not rendered by default.
20.7 KB
Loading
44 KB
Loading
80.6 KB
Loading
68 KB
Loading
62.2 KB
Loading
66 KB
Loading
74.8 KB
Loading

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

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

5-
namespace TodoApi.Controllers
5+
namespace TodoApi.Controllers;
6+
7+
// <snippet_Route>
8+
[Route("api/[controller]")]
9+
[ApiController]
10+
public class TodoItemsController : ControllerBase
11+
// </snippet_Route>
612
{
7-
[Route("api/[controller]")]
8-
[ApiController]
9-
public class TodoItemsController : ControllerBase
13+
private readonly TodoContext _context;
14+
15+
public TodoItemsController(TodoContext context)
1016
{
11-
private readonly TodoContext _context;
17+
_context = context;
18+
}
1219

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

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

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

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

37-
return todoItem;
56+
try
57+
{
58+
await _context.SaveChangesAsync();
3859
}
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)
60+
catch (DbUpdateConcurrencyException)
4661
{
47-
if (id != todoItem.Id)
62+
if (!TodoItemExists(id))
4863
{
49-
return BadRequest();
50-
}
51-
52-
_context.Entry(todoItem).State = EntityState.Modified;
53-
54-
try
55-
{
56-
await _context.SaveChangesAsync();
64+
return NotFound();
5765
}
58-
catch (DbUpdateConcurrencyException)
66+
else
5967
{
60-
if (!TodoItemExists(id))
61-
{
62-
return NotFound();
63-
}
64-
else
65-
{
66-
throw;
67-
}
68+
throw;
6869
}
69-
70-
return NoContent();
7170
}
72-
// </snippet_Update>
7371

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();
72+
return NoContent();
73+
}
74+
// </snippet_Update>
8275

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

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

98-
_context.TodoItems.Remove(todoItem);
99-
await _context.SaveChangesAsync();
101+
_context.TodoItems.Remove(todoItem);
102+
await _context.SaveChangesAsync();
100103

101-
return NoContent();
102-
}
104+
return NoContent();
105+
}
106+
// </snippet_Delete>
103107

104-
private bool TodoItemExists(long id)
105-
{
106-
return _context.TodoItems.Any(e => e.Id == id);
107-
}
108+
private bool TodoItemExists(long id)
109+
{
110+
return _context.TodoItems.Any(e => e.Id == id);
108111
}
109-
}
112+
}
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+
}

0 commit comments

Comments
 (0)