Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private readonly ASPDotNetContext _context;
- public ASPDotNetController(ASPDotNetContext context) {
- _context = context;
- if (_context.ASPDotNetItems.Count() == 0) {
- // Create a new ASPDotNetItem if collection is empty,
- // which means you can't delete all ASPDotNetItems.
- _context.ASPDotNetItems.Add(new ASPDotNetItem { Name = "Item1"});
- _context.ASPDotNetItems.Add(new ASPDotNetItem { Name = "Item2"});
- _context.SaveChanges();
- }
- }
- // GET: api/ASPDotNet
- [HttpGet]
- public async Task<ActionResult<IEnumerable<ASPDotNetItem>>> GetASPDotNetItems() {
- return await _context.ASPDotNetItems.ToListAsync();
- }
- // Get: api/ASPDotNet
- [HttpGet("{id}")]
- public async Task<ActionResult<ASPDotNetItem>> GetASPDotNetItem(long id) {
- var aspDotNetItem = await _context.ASPDotNetItems.FindAsync(id);
- if (aspDotNetItem == null) {
- return NotFound();
- }
- return aspDotNetItem;
- }
- // POST: api/ASPDotNet
- [HttpPost]
- public async Task<ActionResult<ASPDotNetItem>> PostASPDotNetItem(ASPDotNetItem item) {
- _context.ASPDotNetItems.Add(item);
- await _context.SaveChangesAsync();
- return CreatedAtAction(nameof(GetASPDotNetItem), new {id = item.Id}, item);
- }
- // PUT: api/ASPDotNet/3
- [HttpPut("{id}")]
- public async Task<IActionResult> PutASPDotNetItem(long id, ASPDotNetItem item) {
- if (id != item.Id) {
- return BadRequest();
- }
- _context.Entry(item).State = EntityState.Modified;
- await _context.SaveChangesAsync();
- return NoContent();
- }
- // DELETE: api/ASPDotNet/3
- [HttpDelete("{id}")]
- public async Task<IActionResult> DeleteASPDotNetItem(long id) {
- var aspDotNetItem = await _context.ASPDotNetItems.FindAsync(id);
- if (aspDotNetItem == null) {
- return NotFound();
- }
- _context.ASPDotNetItems.Remove(aspDotNetItem);
- await _context.SaveChangesAsync();
- return NoContent();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement