forked from dotnet/AspNetCore.Docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DTO to prevent over-posting on API (dotnet#17074)
* Add DTO to prevent over-posting on API * Add DTO to prevent over-posting on API * Add DTO to prevent over-posting on API * Add DTO to prevent over-posting on API * Add DTO to prevent over-posting on API * Apply suggestions from code review Co-Authored-By: Artak <34246760+mkArtakMSFT@users.noreply.github.com> * Update aspnetcore/tutorials/first-web-api/samples/3.0/TodoApiDTO/Controllers/TodoItemsController.cs Co-Authored-By: Artak <34246760+mkArtakMSFT@users.noreply.github.com> * Add DTO to prevent over-posting on API * Apply suggestions from code review Co-Authored-By: Kirk Larkin <6025110+serpent5@users.noreply.github.com> * Update aspnetcore/tutorials/first-web-api/samples/3.0/TodoApiDTO/Controllers/TodoItemsController.cs Co-Authored-By: Artak <34246760+mkArtakMSFT@users.noreply.github.com> Co-authored-by: Artak <34246760+mkArtakMSFT@users.noreply.github.com> Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com>
- Loading branch information
1 parent
ee40605
commit 227633c
Showing
11 changed files
with
347 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletion
17
aspnetcore/tutorials/first-web-api/samples/3.0/TodoApi/Models/TodoItem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
namespace TodoApi.Models | ||
#define First | ||
|
||
namespace TodoApi.Models | ||
{ | ||
#if First | ||
#region snippet | ||
public class TodoItem | ||
{ | ||
public long Id { get; set; } | ||
public string Name { get; set; } | ||
public bool IsComplete { get; set; } | ||
} | ||
#endregion | ||
#else | ||
// Use this to test you can over-post | ||
public class TodoItem | ||
{ | ||
public long Id { get; set; } | ||
public string Name { get; set; } | ||
public bool IsComplete { get; set; } | ||
public string Secret { get; set; } | ||
} | ||
#endif | ||
} |
142 changes: 142 additions & 0 deletions
142
aspnetcore/tutorials/first-web-api/samples/3.0/TodoApiDTO/Controllers/TodoItemsController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using TodoApi.Models; | ||
|
||
namespace TodoApi.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class TodoItemsController : ControllerBase | ||
{ | ||
private readonly TodoContext _context; | ||
|
||
public TodoItemsController(TodoContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
// GET: api/TodoItems | ||
#region snippet | ||
[HttpGet] | ||
public async Task<ActionResult<IEnumerable<TodoItemDTO>>> GetTodoItems() | ||
{ | ||
return await _context.TodoItems | ||
.Select(x => ItemToDTO(x)) | ||
.ToListAsync(); | ||
} | ||
|
||
[HttpGet("{id}")] | ||
public async Task<ActionResult<TodoItemDTO>> GetTodoItem(long id) | ||
{ | ||
var todoItemDTO = await _context.TodoItems | ||
.Where(x => x.Id == id) | ||
.Select(x => ItemToDTO(x)) | ||
.SingleAsync(); | ||
|
||
if (todoItemDTO == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return todoItemDTO; | ||
} | ||
|
||
[HttpPut("{id}")] | ||
public async Task<IActionResult> UpdateTodoItem(long id, TodoItemDTO todoItemDTO) | ||
{ | ||
if (id != todoItemDTO.Id) | ||
{ | ||
return BadRequest(); | ||
} | ||
|
||
var todoItem = await _context.TodoItems.FindAsync(id); | ||
if (todoItem == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
todoItem.Name = todoItemDTO.Name; | ||
todoItem.IsComplete = todoItemDTO.IsComplete; | ||
|
||
try | ||
{ | ||
await _context.SaveChangesAsync(); | ||
} | ||
catch (DbUpdateConcurrencyException) when (!TodoItemExists(id)) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return NoContent(); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<ActionResult<TodoItem>> CreateTodoItem(TodoItemDTO todoItemDTO) | ||
{ | ||
var todoItem = new TodoItem | ||
{ | ||
IsComplete = todoItemDTO.IsComplete, | ||
Name = todoItemDTO.Name | ||
}; | ||
|
||
_context.TodoItems.Add(todoItem); | ||
await _context.SaveChangesAsync(); | ||
|
||
return CreatedAtAction( | ||
nameof(GetTodoItem), | ||
new { id = todoItem.Id }, | ||
ItemToDTO(todoItem)); | ||
} | ||
|
||
[HttpDelete("{id}")] | ||
public async Task<IActionResult> DeleteTodoItem(long id) | ||
{ | ||
var todoItem = await _context.TodoItems.FindAsync(id); | ||
|
||
if (todoItem == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
_context.TodoItems.Remove(todoItem); | ||
await _context.SaveChangesAsync(); | ||
|
||
return NoContent(); | ||
} | ||
|
||
private bool TodoItemExists(long id) => | ||
_context.TodoItems.Any(e => e.Id == id); | ||
|
||
private static TodoItemDTO ItemToDTO(TodoItem todoItem) => | ||
new TodoItemDTO | ||
{ | ||
Id = todoItem.Id, | ||
Name = todoItem.Name, | ||
IsComplete = todoItem.IsComplete | ||
}; | ||
} | ||
#endregion | ||
} | ||
|
||
/* // This method is just for testing populating the secret field | ||
// POST: api/TodoItems/test | ||
[HttpPost("test")] | ||
public async Task<ActionResult<TodoItem>> PostTestTodoItem(TodoItem todoItem) | ||
{ | ||
_context.TodoItems.Add(todoItem); | ||
await _context.SaveChangesAsync(); | ||
return CreatedAtAction(nameof(GetTodoItem), new { id = todoItem.Id }, todoItem); | ||
} | ||
// This method is just for testing | ||
// GET: api/TodoItems/test | ||
[HttpGet("test")] | ||
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTestTodoItems() | ||
{ | ||
return await _context.TodoItems.ToListAsync(); | ||
} | ||
*/ |
14 changes: 14 additions & 0 deletions
14
aspnetcore/tutorials/first-web-api/samples/3.0/TodoApiDTO/Models/TodoContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace TodoApi.Models | ||
{ | ||
public class TodoContext : DbContext | ||
{ | ||
public TodoContext(DbContextOptions<TodoContext> options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public DbSet<TodoItem> TodoItems { get; set; } | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
aspnetcore/tutorials/first-web-api/samples/3.0/TodoApiDTO/Models/TodoItem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace TodoApi.Models | ||
{ | ||
#region snippet | ||
public class TodoItem | ||
{ | ||
public long Id { get; set; } | ||
public string Name { get; set; } | ||
public bool IsComplete { get; set; } | ||
public string Secret { get; set; } | ||
} | ||
#endregion | ||
} |
11 changes: 11 additions & 0 deletions
11
aspnetcore/tutorials/first-web-api/samples/3.0/TodoApiDTO/Models/TodoItemDTO.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace TodoApi.Models | ||
{ | ||
#region snippet | ||
public class TodoItemDTO | ||
{ | ||
public long Id { get; set; } | ||
public string Name { get; set; } | ||
public bool IsComplete { get; set; } | ||
} | ||
#endregion | ||
} |
26 changes: 26 additions & 0 deletions
26
aspnetcore/tutorials/first-web-api/samples/3.0/TodoApiDTO/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace TodoApi | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
aspnetcore/tutorials/first-web-api/samples/3.0/TodoApiDTO/Startup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.HttpsPolicy; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using TodoApi.Models; | ||
|
||
namespace TodoApi | ||
{ | ||
public class Startup | ||
{ | ||
public Startup(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
|
||
public IConfiguration Configuration { get; } | ||
|
||
// This method gets called by the runtime. Use this method to add services to the container. | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddDbContext<TodoContext>(opt => | ||
opt.UseInMemoryDatabase("TodoList")); | ||
services.AddControllers(); | ||
} | ||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseRouting(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapControllers(); | ||
}); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
aspnetcore/tutorials/first-web-api/samples/3.0/TodoApiDTO/TodoApiDTO.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" /> | ||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" /> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
9 changes: 9 additions & 0 deletions
9
aspnetcore/tutorials/first-web-api/samples/3.0/TodoApiDTO/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
aspnetcore/tutorials/first-web-api/samples/3.0/TodoApiDTO/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |