ASP.NET Core๋ก ์น API๋ฅผ ๋น๋ํ๋ ๊ณผ์ ์ ์ค๋ช ํฉ๋๋ค.
- ASP.NET ๋ฐ ์น ๊ฐ๋ฐ ํฌํจ๋ Visual Studio 2019 16.8 ์ด์
- .NET 5.0 SDK ์ด์
- ASP.NET CORE ํ๋ก์ ํธ ์์ฑ
- Model๋ก ์ฌ์ฉํ Class์ EntityFrameWork DataBase Context ์ถ๊ฐ
- CRUD๋ฅผ ์ํ ๋ฉ์๋๋ก ์ค์บํด๋ ์ปจํธ๋กค๋ฌ ์์ฑ
- ๋ผ์ฐํ , URL ๊ฒฝ๋ก ๋ฐ ๋ฉ์๋ ๋ฐํ ๊ฐ ๊ตฌ์ฑ
- PostMan์ ์ด์ฉํ API ํธ์ถ ํ ์คํธ
-
๋น์ฃผ์ผ ์คํ๋์ค์์ ์๋ก ๋ง๋ค๊ธฐ > ์ ํ๋ก์ ํธ > ASP.NET CORE ์น ์ ํ๋ฆฌ์ผ์ด์
-
AspCoreApi๋ก ํ๋ก์ ํธ ์ด๋ฆ ์ค์ ๋ค API ํญ๋ชฉ์ ์ ํํด ํ๋ก์ ํธ ์์ฑ
launchUrl์ ํญ๋ชฉ์ ์ฌ์ฉํ api ์ฃผ์๋ก ๋ณ๊ฒฝํด์ค๋๋ค.
Properties/launchSetting.json
"launchUrl": "api/TodoItems",
๋ชจ๋ธ์ ์ฑ์์ ๊ด๋ฆฌํ ๋ฐ์ดํฐ๋ฅผ ๋ํ๋ด๋ ํด๋์ค์ ์งํฉ์ ๋๋ค.
- ํ๋ก์ ํธ์์ ์ถ๊ฐ > ์ํด๋ ํด๋ ์ด๋ฆ์ Model๋ก ์์ฑ
- Model ํด๋์์ ์ถ๊ฐ > ํด๋์ค ํด๋์ค ์ด๋ฆ TodoItem์ผ๋ก ์์ฑ
TodoItem.cs
namespace AspCoreApi.Models
{
public class TodoItem
{
public long Id { get; set; }
public string Name { get; set; }
public bool IsComplete { get; set; }
}
}
-
id๋ Primary Key ์ญํ ์ ํฉ๋๋ค.
DataBase Context๋ ๋ฐ์ดํฐ ๋ชจ๋ธ์ ๋ํ ์ํฐํฐ ํ๋ ์ ์ํฌ์ ๊ธฐ๋ฅ์ ์กฐ์ ํ๋ ๋ฉ์ธ ํด๋์ค์ ๋๋ค.
-
์๋ฃจ์ ์์ ์ค๋ฅธ์ชฝ ํด๋ฆญ์ผ๋ก Nuget ํจํค์ง ๊ด๋ฆฌ์ ์ง์
-
์ฐพ์๋ณด๊ธฐ ํญ์ ๋๋ฅธ๋ค์ Microsoft.EntityFrameworkCore.SqlServer ์ ๋ ฅํ ํด๋น Nuget์ ์ ํ ํ ์ค์น
-
Models ํด๋์์ ์ถ๊ฐ > ํด๋์ค ํด๋์ค ์ด๋ฆ ApiDBContext ์์ฑ
-
ApiDBContext.cs
using Microsoft.EntityFrameworkCore;
namespace AspCoreApi.Models
{
public class ApiDBContext : DbContext
{
public ApiDBContext(DbContextOptions<ApiDBContext> options) : base(options)
{
}
public DbSet<TodoItem> TodoItem { get; set; }
}
}
ASP.NET Core์์ DB Context์ ๊ฐ์ ์๋น์ค๋ DI(์ข ์์ฑ ์ฃผ์ ) ์ปจํ ์ด๋์ ๋ฑ๋ก ๋์ด์ผํฉ๋๋ค.
StartUp.cs
using AspCoreApi.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
namespace AspCoreApi
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApiDBContext>(opt =>
opt.UseInMemoryDatabase("TodoList"));
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
- Controllers ํด๋์์ ์ถ๊ฐ > ์ค์บํด๋ ํญ๋ชฉ ์๋ก ๋ง๋ค๊ธฐ(F) ์ ํ
- Entity Framework๋ฅผ ์ฌ์ฉํ๋ฉฐ ๋์์ด ํฌํจ๋ API ์ปจํธ๋กค๋ฌ ์ถ๊ฐ
- Model๊ณผ ํด๋น Model์ Database Context๋ฅผ ์ ํ ํด์ค๋๋ค. (์์ ๋ ์๋์ ๊ฐ์ต๋๋ค.)
- ๋ชจ๋ธ ํด๋์ค์์ TodoItem (AspCoreApi.Models) ๋ฅผ ์ ํํฉ๋๋ค.
- ๋ฐ์ดํฐ ์ปจํ
์คํธ ํด๋์ค์์ ApiDBContext (AspCoreApi.Models) ๋ฅผ ์ ํํฉ๋๋ค.
์๋์ ๊ฐ์ด Controller ์ฝ๋๋ฅผ ์์ ํฉ๋๋ค.
TodoItemsController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using AspCoreApi.Models;
namespace AspCoreApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TodoItemsController : ControllerBase
{
private readonly ApiDBContext _context;
public TodoItemsController(ApiDBContext context)
{
_context = context;
}
// GET: api/TodoItems
[HttpGet]
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItem()
{
return await _context.TodoItem.ToListAsync();
}
// GET: api/TodoItems/5
[HttpGet("{id}")]
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
{
var todoItem = await _context.TodoItem.FindAsync(id);
if (todoItem == null)
{
return NotFound();
}
return todoItem;
}
// PUT: api/TodoItems/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPut("{id}")]
public async Task<IActionResult> PutTodoItem(long id, TodoItem todoItem)
{
if (id != todoItem.Id)
{
return BadRequest();
}
_context.Entry(todoItem).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TodoItemExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/TodoItems
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
// POST: api/TodoItems
[HttpPost]
public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
{
_context.TodoItem.Add(todoItem);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetTodoItem), new { id = todoItem.Id }, todoItem);
}
// DELETE: api/TodoItems/5
[HttpDelete("{id}")]
public async Task<ActionResult<TodoItem>> DeleteTodoItem(long id)
{
var todoItem = await _context.TodoItem.FindAsync(id);
if (todoItem == null)
{
return NotFound();
}
_context.TodoItem.Remove(todoItem);
await _context.SaveChangesAsync();
return todoItem;
}
private bool TodoItemExists(long id)
{
return _context.TodoItem.Any(e => e.Id == id);
}
}
}
- ์์ ์ฝ๋ ๋ด์ฉ์ ์๋์ ๊ฐ์ต๋๋ค.
API | ๊ธฐ์ | ์์ฒญ ๋ณธ๋ฌธ | ์๋ต ๋ณธ๋ฌธ |
---|---|---|---|
GET /api/ | ๋ชจ๋ ํ ์ผ ํญ๋ชฉ ๊ฐ์ ธ ์ค๊ธฐ | ์์ | ํ ์ผ ํญ๋ชฉ ๋ฐฐ์ด |
GET /api/TodoItems/{id} | ID๋ก ํญ๋ชฉ ๋ฐ๊ธฐ | ์์ | ํ ์ผ ํญ๋ชฉ |
POST /api/TodoItems | ์ ํญ๋ชฉ ์ถ๊ฐ | ํ ์ผ ํญ๋ชฉ | ํ ์ผ ํญ๋ชฉ |
PUT /api/TodoItems/{id} | ๊ธฐ์กด ํญ๋ชฉ ์ ๋ฐ์ดํธ | ํ ์ผ ํญ๋ชฉ | ์์ |
DELETE /api/TodoItems/{id} | ํญ๋ชฉ ์ญ์ | ์์ | ์์ |
PostMan ์ค์น PostMan์ ์ฌ์ฉํ์ฌ ์์ ์์ฑํ ์น API๋ฅผ ํ ์คํธํฉ๋๋ค.
- HTTP ๋ฉ์๋๋ฅผ POST๋ก ๋ณ๊ฒฝ
- URI ์ค์ http://localhost:ํฌํธ๋ฒํธ/api/TodoItems (ํฌํธ ๋ฒํธ๋ ํ๋ก์ ํธ์ Properties/launchSetting.json ํ์ผ์ iisSettings์์ฑ์์ ํ์ธํ ์ ์์ต๋๋ค.)
- Body ํญ ์ ํ
- raw ๋ผ๋์ค ๋ฒํผ ์ ํ
- JSON์ผ๋ก ์ ํ ๋ณ๊ฒฝ Body์ ์๋์ ๊ฐ์ด ์ ๋ ฅ
{
"id": 1,
"name": "API ํ
์คํธ",
"isComplete": true
}
- Send ํด๋ฆญ
์์ ๋จ๊ณ๋ฅผ ์ํํ๋ฉด ์๋์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ค๋๋ค.