Skip to content

Commit 84b365e

Browse files
Webapi update to 5.0/ra (#19781)
* Update first Web API to 5.0 RC1 * Update first Web API to .NET 5 RC1 * Update first Web API to .NET 5 RC1 * Update first Web API to .NET 5 RC1 * work * work * work * clean up * clean up * clean up * clean up
1 parent e550a68 commit 84b365e

24 files changed

Lines changed: 1218 additions & 34 deletions

aspnetcore/tutorials/first-web-api.md

Lines changed: 560 additions & 34 deletions
Large diffs are not rendered by default.
122 KB
Loading
182 KB
Loading
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.EntityFrameworkCore;
8+
using TodoApi.Models;
9+
10+
namespace TodoApi.Controllers
11+
{
12+
#region TodoController
13+
[Route("api/[controller]")]
14+
[ApiController]
15+
public class TodoItemsController : ControllerBase
16+
{
17+
private readonly TodoContext _context;
18+
19+
public TodoItemsController(TodoContext context)
20+
{
21+
_context = context;
22+
}
23+
#endregion
24+
25+
// GET: api/TodoItems
26+
[HttpGet]
27+
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
28+
{
29+
return await _context.TodoItems.ToListAsync();
30+
}
31+
32+
// GET: api/TodoItems/5
33+
[HttpGet("{id}")]
34+
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
35+
{
36+
var todoItem = await _context.TodoItems.FindAsync(id);
37+
38+
if (todoItem == null)
39+
{
40+
return NotFound();
41+
}
42+
43+
return todoItem;
44+
}
45+
46+
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
47+
#region snippet_Update
48+
// PUT: api/TodoItems/5
49+
[HttpPut("{id}")]
50+
public async Task<IActionResult> PutTodoItem(long id, TodoItem todoItem)
51+
{
52+
if (id != todoItem.Id)
53+
{
54+
return BadRequest();
55+
}
56+
57+
_context.Entry(todoItem).State = EntityState.Modified;
58+
59+
try
60+
{
61+
await _context.SaveChangesAsync();
62+
}
63+
catch (DbUpdateConcurrencyException)
64+
{
65+
if (!TodoItemExists(id))
66+
{
67+
return NotFound();
68+
}
69+
else
70+
{
71+
throw;
72+
}
73+
}
74+
75+
return NoContent();
76+
}
77+
#endregion
78+
79+
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
80+
#region snippet_Create
81+
// POST: api/TodoItems
82+
[HttpPost]
83+
public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
84+
{
85+
_context.TodoItems.Add(todoItem);
86+
await _context.SaveChangesAsync();
87+
88+
//return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem);
89+
return CreatedAtAction(nameof(GetTodoItem), new { id = todoItem.Id }, todoItem);
90+
}
91+
#endregion
92+
93+
#region snippet_Delete
94+
// DELETE: api/TodoItems/5
95+
[HttpDelete("{id}")]
96+
public async Task<IActionResult> DeleteTodoItem(long id)
97+
{
98+
var todoItem = await _context.TodoItems.FindAsync(id);
99+
if (todoItem == null)
100+
{
101+
return NotFound();
102+
}
103+
104+
_context.TodoItems.Remove(todoItem);
105+
await _context.SaveChangesAsync();
106+
107+
return NoContent();
108+
}
109+
#endregion
110+
111+
private bool TodoItemExists(long id)
112+
{
113+
return _context.TodoItems.Any(e => e.Id == id);
114+
}
115+
}
116+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace TodoApi.Controllers
9+
{
10+
[ApiController]
11+
[Route("[controller]")]
12+
public class WeatherForecastController : ControllerBase
13+
{
14+
private static readonly string[] Summaries = new[]
15+
{
16+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
17+
};
18+
19+
private readonly ILogger<WeatherForecastController> _logger;
20+
21+
// The Web API will only accept tokens 1) for users, and 2) having the access_as_user scope for this API
22+
static readonly string[] scopeRequiredByApi = new string[] { "access_as_user" };
23+
24+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
25+
{
26+
_logger = logger;
27+
}
28+
29+
[HttpGet]
30+
public IEnumerable<WeatherForecast> Get()
31+
{
32+
var rng = new Random();
33+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
34+
{
35+
Date = DateTime.Now.AddDays(index),
36+
TemperatureC = rng.Next(-20, 55),
37+
Summary = Summaries[rng.Next(Summaries.Length)]
38+
})
39+
.ToArray();
40+
}
41+
}
42+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace TodoApi.Models
4+
{
5+
public class TodoContext : DbContext
6+
{
7+
public TodoContext(DbContextOptions<TodoContext> options)
8+
: base(options)
9+
{
10+
}
11+
12+
public DbSet<TodoItem> TodoItems { get; set; }
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#region snippet
2+
namespace TodoApi.Models
3+
{
4+
public class TodoItem
5+
{
6+
public long Id { get; set; }
7+
public string Name { get; set; }
8+
public bool IsComplete { get; set; }
9+
}
10+
}
11+
#endregion
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace TodoApi
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#region snippet_all
2+
// Unused usings removed
3+
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.EntityFrameworkCore;
9+
using TodoApi.Models;
10+
11+
namespace TodoApi
12+
{
13+
public class Startup
14+
{
15+
public Startup(IConfiguration configuration)
16+
{
17+
Configuration = configuration;
18+
}
19+
20+
public IConfiguration Configuration { get; }
21+
22+
public void ConfigureServices(IServiceCollection services)
23+
{
24+
services.AddDbContext<TodoContext>(opt =>
25+
opt.UseInMemoryDatabase("TodoList"));
26+
services.AddControllers();
27+
}
28+
29+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
30+
{
31+
if (env.IsDevelopment())
32+
{
33+
app.UseDeveloperExceptionPage();
34+
}
35+
36+
app.UseHttpsRedirection();
37+
app.UseRouting();
38+
39+
app.UseAuthorization();
40+
41+
app.UseEndpoints(endpoints =>
42+
{
43+
endpoints.MapControllers();
44+
});
45+
}
46+
}
47+
}
48+
#endregion
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.0-alpha.1.20417.8" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0-alpha.1.20417.8" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-alpha.1.20417.8" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-alpha.1.20417.8">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
</PackageReference>
15+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.0-rc.1.20458.1" />
16+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.1" />
17+
<PackageReference Include="System.IO.Pipelines" Version="5.0.0-rc.2.20454.25" />
18+
</ItemGroup>
19+
20+
</Project>

0 commit comments

Comments
 (0)