From ec524e7b926fe658f7850983f51859bbc7d2a634 Mon Sep 17 00:00:00 2001 From: Tom Dykstra <1569635+tdykstra@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:24:35 -0800 Subject: [PATCH 1/2] add GetById --- .../WebApiSample/Controllers/ProductsController.cs | 13 +++++++++++++ .../8.x/WebApiSample/WebApiSample.csproj | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/mvc/action-return-types/8.x/WebApiSample/Controllers/ProductsController.cs b/mvc/action-return-types/8.x/WebApiSample/Controllers/ProductsController.cs index f7c8585..adce20d 100644 --- a/mvc/action-return-types/8.x/WebApiSample/Controllers/ProductsController.cs +++ b/mvc/action-return-types/8.x/WebApiSample/Controllers/ProductsController.cs @@ -52,4 +52,17 @@ public async IAsyncEnumerable GetOnSaleProductsAsync() } } // + + [HttpGet("{id}")] + public async Task> GetByIdAsync(int id) + { + var product = await _productContext.Products.FindAsync(id); + + if (product == null) + { + return NotFound(); + } + + return product; + } } diff --git a/mvc/action-return-types/8.x/WebApiSample/WebApiSample.csproj b/mvc/action-return-types/8.x/WebApiSample/WebApiSample.csproj index 919b563..8d64552 100644 --- a/mvc/action-return-types/8.x/WebApiSample/WebApiSample.csproj +++ b/mvc/action-return-types/8.x/WebApiSample/WebApiSample.csproj @@ -7,8 +7,8 @@ - - + + From 4a3b5036d2d31f05059cd4e44820a2c51e0d6264 Mon Sep 17 00:00:00 2001 From: Tom Dykstra <1569635+tdykstra@users.noreply.github.com> Date: Tue, 17 Dec 2024 14:30:09 -0800 Subject: [PATCH 2/2] CreatedAt --> GetById --- .../ActionResultOfTProductsController.cs | 2 +- .../WebApiSample/Controllers/ProductsController.cs | 13 ------------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/mvc/action-return-types/8.x/WebApiSample/Controllers/ActionResultOfTProductsController.cs b/mvc/action-return-types/8.x/WebApiSample/Controllers/ActionResultOfTProductsController.cs index 58f7134..0216d54 100644 --- a/mvc/action-return-types/8.x/WebApiSample/Controllers/ActionResultOfTProductsController.cs +++ b/mvc/action-return-types/8.x/WebApiSample/Controllers/ActionResultOfTProductsController.cs @@ -41,7 +41,7 @@ public async Task> CreateAsync_ActionResultOfT(Product pro _productContext.Products.Add(product); await _productContext.SaveChangesAsync(); - return CreatedAtAction(nameof(CreateAsync_ActionResultOfT), new { id = product.Id }, product); + return CreatedAtAction(nameof(GetById_ActionResultOfT), new { id = product.Id }, product); } // } diff --git a/mvc/action-return-types/8.x/WebApiSample/Controllers/ProductsController.cs b/mvc/action-return-types/8.x/WebApiSample/Controllers/ProductsController.cs index adce20d..f7c8585 100644 --- a/mvc/action-return-types/8.x/WebApiSample/Controllers/ProductsController.cs +++ b/mvc/action-return-types/8.x/WebApiSample/Controllers/ProductsController.cs @@ -52,17 +52,4 @@ public async IAsyncEnumerable GetOnSaleProductsAsync() } } // - - [HttpGet("{id}")] - public async Task> GetByIdAsync(int id) - { - var product = await _productContext.Products.FindAsync(id); - - if (product == null) - { - return NotFound(); - } - - return product; - } }