parent
d4d5e6e3e9
commit
64525d8f08
|
@ -94,7 +94,7 @@ Consider the following synchronous action in which there are two possible return
|
|||
|
||||
::: moniker range=">= aspnetcore-2.1"
|
||||
|
||||
[!code-csharp[](../web-api/action-return-types/samples/2x/WebApiSample.Api.21/Controllers/ProductsController.cs?name=snippet_GetById&highlight=8,11)]
|
||||
[!code-csharp[](../web-api/action-return-types/samples/2x/WebApiSample.Api.21/Controllers/ProductsController.cs?name=snippet_GetByIdIActionResult&highlight=8,11)]
|
||||
|
||||
::: moniker-end
|
||||
|
||||
|
@ -115,13 +115,13 @@ Consider the following asynchronous action in which there are two possible retur
|
|||
|
||||
::: moniker range=">= aspnetcore-2.1"
|
||||
|
||||
[!code-csharp[](../web-api/action-return-types/samples/2x/WebApiSample.Api.21/Controllers/ProductsController.cs?name=snippet_CreateAsync&highlight=8,13)]
|
||||
[!code-csharp[](../web-api/action-return-types/samples/2x/WebApiSample.Api.21/Controllers/ProductsController.cs?name=snippet_CreateAsyncIActionResult&highlight=9,14)]
|
||||
|
||||
::: moniker-end
|
||||
|
||||
::: moniker range="<= aspnetcore-2.0"
|
||||
|
||||
[!code-csharp[](../web-api/action-return-types/samples/2x/WebApiSample.Api.Pre21/Controllers/ProductsController.cs?name=snippet_CreateAsync&highlight=8,13)]
|
||||
[!code-csharp[](../web-api/action-return-types/samples/2x/WebApiSample.Api.Pre21/Controllers/ProductsController.cs?name=snippet_CreateAsync&highlight=9,14)]
|
||||
|
||||
::: moniker-end
|
||||
|
||||
|
@ -161,7 +161,7 @@ Most actions have a specific return type. Unexpected conditions can occur during
|
|||
|
||||
Consider a synchronous action in which there are two possible return types:
|
||||
|
||||
[!code-csharp[](../web-api/action-return-types/samples/2x/WebApiSample.Api.21/Controllers/ProductsController.cs?name=snippet_GetById&highlight=7,10)]
|
||||
[!code-csharp[](../web-api/action-return-types/samples/2x/WebApiSample.Api.21/Controllers/ProductsController.cs?name=snippet_GetByIdActionResult&highlight=8,11)]
|
||||
|
||||
In the preceding action:
|
||||
|
||||
|
@ -172,7 +172,7 @@ In the preceding action:
|
|||
|
||||
Consider an asynchronous action in which there are two possible return types:
|
||||
|
||||
[!code-csharp[](../web-api/action-return-types/samples/2x/WebApiSample.Api.21/Controllers/ProductsController.cs?name=snippet_CreateAsync&highlight=8,13)]
|
||||
[!code-csharp[](../web-api/action-return-types/samples/2x/WebApiSample.Api.21/Controllers/ProductsController.cs?name=snippet_CreateAsyncActionResult&highlight=9,14)]
|
||||
|
||||
In the preceding action:
|
||||
|
||||
|
|
|
@ -26,7 +26,22 @@ namespace WebApiSample.Controllers
|
|||
_repository.GetProducts();
|
||||
#endregion
|
||||
|
||||
#region snippet_GetById
|
||||
#region snippet_GetByIdIActionResult
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public IActionResult<Product> GetById(int id)
|
||||
{
|
||||
if (!_repository.TryGetProduct(id, out var product))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return product;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region snippet_GetByIdActionResult
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
|
@ -41,7 +56,25 @@ namespace WebApiSample.Controllers
|
|||
}
|
||||
#endregion
|
||||
|
||||
#region snippet_CreateAsync
|
||||
#region snippet_CreateAsyncIActionResult
|
||||
[HttpPost]
|
||||
[Consumes(MediaTypeNames.Application.Json)]
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult<Product>> CreateAsync(Product product)
|
||||
{
|
||||
if (product.Description.Contains("XYZ Widget"))
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
await _repository.AddProductAsync(product);
|
||||
|
||||
return CreatedAtAction(nameof(GetById), new { id = product.Id }, product);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region snippet_CreateAsyncActionResult
|
||||
[HttpPost]
|
||||
[Consumes(MediaTypeNames.Application.Json)]
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
|
|
Loading…
Reference in New Issue