Created
August 21, 2022 22:35
ProductsController v2 and v3
This file contains hidden or 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
[ApiVersion("2.0")] | |
[ApiVersion("3.0")] | |
[Route("api/v{version:apiVersion}/[controller]")] | |
[ApiController] | |
public class ProductsController : ControllerBase | |
{ | |
private readonly ECommerceDbContext _context; | |
public ProductsController(ECommerceDbContext context) | |
{ | |
_context = context; | |
_context.Database.EnsureCreated(); | |
} | |
[HttpGet] | |
public async Task<ActionResult> GetAllProducts() | |
{ | |
IQueryable<Product> products = _context.Products.Take(3); | |
return Ok(await products.ToArrayAsync()); | |
} | |
[HttpGet] | |
[MapToApiVersion("3.0")] | |
public async Task<ActionResult> GetAllProductsV3() | |
{ | |
IQueryable<Product> products = _context.Products.Take(10); | |
return Ok(await products.ToArrayAsync()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment