SHARE
TWEET

Untitled

a guest Oct 21st, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private readonly ASPDotNetContext _context;
  2.  
  3.         public ASPDotNetController(ASPDotNetContext context) {
  4.             _context = context;
  5.        
  6.             if (_context.ASPDotNetItems.Count() == 0) {
  7.                
  8.                 // Create a new ASPDotNetItem if collection is empty,
  9.                 // which means you can't delete all ASPDotNetItems.
  10.                 _context.ASPDotNetItems.Add(new ASPDotNetItem { Name = "Item1"});
  11.                 _context.ASPDotNetItems.Add(new ASPDotNetItem { Name = "Item2"});
  12.                 _context.SaveChanges();
  13.             }
  14.         }
  15.  
  16.         // GET: api/ASPDotNet
  17.         [HttpGet]
  18.         public async Task<ActionResult<IEnumerable<ASPDotNetItem>>> GetASPDotNetItems() {
  19.             return await _context.ASPDotNetItems.ToListAsync();
  20.         }
  21.  
  22.         // Get: api/ASPDotNet
  23.         [HttpGet("{id}")]
  24.         public async Task<ActionResult<ASPDotNetItem>> GetASPDotNetItem(long id) {
  25.  
  26.             var aspDotNetItem = await _context.ASPDotNetItems.FindAsync(id);
  27.  
  28.             if (aspDotNetItem == null) {
  29.                 return NotFound();
  30.             }
  31.  
  32.             return aspDotNetItem;
  33.  
  34.         }
  35.  
  36.         // POST: api/ASPDotNet
  37.         [HttpPost]
  38.         public async Task<ActionResult<ASPDotNetItem>> PostASPDotNetItem(ASPDotNetItem item) {
  39.            
  40.             _context.ASPDotNetItems.Add(item);
  41.             await _context.SaveChangesAsync();
  42.  
  43.             return CreatedAtAction(nameof(GetASPDotNetItem), new {id = item.Id}, item);
  44.         }
  45.  
  46.         // PUT: api/ASPDotNet/3
  47.         [HttpPut("{id}")]
  48.         public async Task<IActionResult> PutASPDotNetItem(long id, ASPDotNetItem item) {
  49.            
  50.             if (id != item.Id) {
  51.                 return BadRequest();
  52.             }
  53.  
  54.             _context.Entry(item).State = EntityState.Modified;
  55.             await _context.SaveChangesAsync();
  56.  
  57.             return NoContent();
  58.         }
  59.  
  60.         // DELETE: api/ASPDotNet/3
  61.         [HttpDelete("{id}")]
  62.         public async Task<IActionResult> DeleteASPDotNetItem(long id) {
  63.  
  64.             var aspDotNetItem = await _context.ASPDotNetItems.FindAsync(id);
  65.  
  66.             if (aspDotNetItem == null) {
  67.                 return NotFound();
  68.             }
  69.  
  70.             _context.ASPDotNetItems.Remove(aspDotNetItem);
  71.             await _context.SaveChangesAsync();
  72.  
  73.             return NoContent();
  74.         }
  75.  
  76.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top