Created
June 5, 2018 00:17
-
-
Save pranavkm/fbb590e031dbd0d619c3b1e01209691d to your computer and use it in GitHub Desktop.
Stripped down copy of scaffolded controller using Entity Framework
This file contains 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
// GET: api/Person | |
[HttpGet] | |
public IEnumerable<Person> GetPerson() | |
{ | |
return _context.Person; | |
} | |
// GET: api/Person/5 | |
[HttpGet("{id}")] | |
public async Task<IActionResult> GetPerson([FromRoute] int id) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var person = await _context.Person.FindAsync(id); | |
if (person == null) | |
{ | |
return NotFound(); | |
} | |
return Ok(person); | |
} | |
// PUT: api/Person/5 | |
[HttpPut("{id}")] | |
public async Task<IActionResult> PutPerson([FromRoute] int id, [FromBody] Person person) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
if (!PersonExists(id)) | |
{ | |
return NotFound(); | |
} | |
return NoContent(); | |
} | |
// POST: api/Person | |
[HttpPost] | |
public async Task<IActionResult> PostPerson([FromBody] Person person) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
return CreatedAtAction("GetPerson", new { id = person.Id }, person); | |
} | |
// DELETE: api/Person/5 | |
[HttpDelete("{id}")] | |
public async Task<IActionResult> DeletePerson([FromRoute] int id) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var person = await _context.Person.FindAsync(id); | |
if (person == null) | |
{ | |
return NotFound(); | |
} | |
return Ok(person); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment