Created
February 16, 2019 10:44
-
-
Save fugaku/e5397dc19b1f417d7a6c8785476ffd31 to your computer and use it in GitHub Desktop.
EF Core Getting Started
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
| public class BlogsController : ControllerBase | |
| { | |
| private readonly AppContext _context; | |
| public BlogsController(AppContext context) | |
| { | |
| _context = context; | |
| } | |
| // GET: api/Blogs | |
| [HttpGet] | |
| public async Task<ActionResult<IEnumerable<Blog>>> GetBlogs() | |
| { | |
| return await _context.Blogs.ToListAsync(); | |
| } | |
| // GET: api/Blogs/5 | |
| [HttpGet("{id}")] | |
| public async Task<ActionResult<Blog>> GetBlog(int id) | |
| { | |
| var blog = await _context.Blogs.FindAsync(id); | |
| if (blog == null) | |
| { | |
| return NotFound(); | |
| } | |
| return blog; | |
| } | |
| [HttpPost] | |
| public async Task<ActionResult<Blog>> PostBlog(Blog blog) | |
| { | |
| _context.Blogs.Add(blog); | |
| await _context.SaveChangesAsync(); | |
| return CreatedAtAction("GetBlog", new { id = blog.BlogId }, blog); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment