Skip to content

LimChaeJune/ASP.NET-CORE-Web-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

13 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

ASP.NET CORE๋กœ API ์ œ์ž‘ํ•˜๊ธฐ

๊ฐœ์š”

ASP.NET Core๋กœ ์›น API๋ฅผ ๋นŒ๋“œํ•˜๋Š” ๊ณผ์ •์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

์‹œ์ž‘ํ•˜๊ธฐ

  • ASP.NET ๋ฐ ์›น ๊ฐœ๋ฐœ ํฌํ•จ๋œ Visual Studio 2019 16.8 ์ด์ƒ
  • .NET 5.0 SDK ์ด์ƒ
  1. ASP.NET CORE ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ
  2. Model๋กœ ์‚ฌ์šฉํ•  Class์™€ EntityFrameWork DataBase Context ์ถ”๊ฐ€
  3. CRUD๋ฅผ ์œ„ํ•œ ๋ฉ”์„œ๋“œ๋กœ ์Šค์บํด๋“œ ์ปจํŠธ๋กค๋Ÿฌ ์ƒ์„ฑ
  4. ๋ผ์šฐํŒ…, URL ๊ฒฝ๋กœ ๋ฐ ๋ฉ”์„œ๋“œ ๋ฐ˜ํ™˜ ๊ฐ’ ๊ตฌ์„ฑ
  5. PostMan์„ ์ด์šฉํ•œ API ํ˜ธ์ถœ ํ…Œ์ŠคํŠธ

1. ASP.NET CORE ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

  • ๋น„์ฃผ์–ผ ์ŠคํŠœ๋””์˜ค์—์„œ ์ƒˆ๋กœ ๋งŒ๋“ค๊ธฐ > ์ƒˆ ํ”„๋กœ์ ํŠธ > ASP.NET CORE ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜

  • AspCoreApi๋กœ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„ ์„ค์ • ๋’ค API ํ•ญ๋ชฉ์„ ์„ ํƒํ•ด ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ 1 2 3

    1.1 launchUrl ์—…๋ฐ์ดํŠธ

    launchUrl์˜ ํ•ญ๋ชฉ์„ ์‚ฌ์šฉํ•  api ์ฃผ์†Œ๋กœ ๋ณ€๊ฒฝํ•ด์ค๋‹ˆ๋‹ค.

    Properties/launchSetting.json

    "launchUrl": "api/TodoItems",

2. Model Class ์ถ”๊ฐ€

๋ชจ๋ธ์€ ์•ฑ์—์„œ ๊ด€๋ฆฌํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค์˜ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค.

  • ํ”„๋กœ์ ํŠธ์—์„œ ์ถ”๊ฐ€ > ์ƒˆํด๋” ํด๋” ์ด๋ฆ„์€ 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 ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.


    3. DataBase Context ์ถ”๊ฐ€

    DataBase Context๋Š” ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์— ๋Œ€ํ•œ ์—”ํ‹ฐํ‹ฐ ํ”„๋ ˆ์ž„ ์›Œํฌ์˜ ๊ธฐ๋Šฅ์„ ์กฐ์ •ํ•˜๋Š” ๋ฉ”์ธ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

    Nuget ํŒจํ‚ค์ง€ ์ถ”๊ฐ€

    • ์†”๋ฃจ์…˜์—์„œ ์˜ค๋ฅธ์ชฝ ํด๋ฆญ์œผ๋กœ 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; }
    }
}

4. DataBase Context ๋“ฑ๋ก

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();
            });
        }
    }
}

5. ์Šค์บ ํด๋“œ ์ปจํŠธ๋กค๋Ÿฌ ์ถ”๊ฐ€

  • 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} ํ•ญ๋ชฉ ์‚ญ์ œ ์—†์Œ ์—†์Œ

6. Postman์„ ์ด์šฉํ•œ ํ…Œ์ŠคํŠธ

PostMan ์„ค์น˜ PostMan์„ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์— ์ƒ์„ฑํ•œ ์›น API๋ฅผ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค.

6.1 Post TodoItem

  • HTTP ๋ฉ”์„œ๋“œ๋ฅผ POST๋กœ ๋ณ€๊ฒฝ
  • URI ์„ค์ • http://localhost:ํฌํŠธ๋ฒˆํ˜ธ/api/TodoItems (ํฌํŠธ ๋ฒˆํ˜ธ๋Š” ํ”„๋กœ์ ํŠธ์˜ Properties/launchSetting.json ํŒŒ์ผ์˜ iisSettings์†์„ฑ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)
  • Body ํƒญ ์„ ํƒ
  • raw ๋ผ๋””์˜ค ๋ฒ„ํŠผ ์„ ํƒ
  • JSON์œผ๋กœ ์œ ํ˜• ๋ณ€๊ฒฝ Body์˜ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅ
{
  "id": 1,
  "name": "API ํ…Œ์ŠคํŠธ",
  "isComplete": true
}
  • Send ํด๋ฆญ

์œ„์— ๋‹จ๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 1

About

This Repository Has Using ASP.NET-CORE Web API Build

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages