天天看點

.net 5 webapi開發入門上建立項目

https://docs.microsoft.com/zh-cn/learn/modules/build-web-api-aspnet-core/

建立項目

1.vscode打開項目檔案夾,指令行建立模闆項目并運作

dotnet new webapi --no-https
dotnet run
           

2.使用工具調試接口

dotnet tool install -g Microsoft.dotnet-httprepl
           

Microsoft.dotnet-httprepl是一個可以調試接口,對接口進行連接配接,檢視接口清單,調用GET,POST的指令行工具

httprepl http://localhost:5000  //連接配接接口
ls  						//列出接口
cd iface  					//路由到接口URI
get   						//對接口執行GET
exit 						//退出
           

3. 建立模型 Models/Pizza.cs

namespace ContosoPizza.Models
{
    public class Pizza
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool IsGlutenFree { get; set; }
    }
}
           

4.建立資料存儲輔助類 Services/PizzaService.cs(在記憶體中讀取記錄,可用資料庫代替)

using ContosoPizza.Models;
using System.Collections.Generic;
using System.Linq;

namespace ContosoPizza.Services
{
    public static class PizzaService
    {
        static List<Pizza> Pizzas { get; }
        static int nextId = 3;
        static PizzaService()
        {
            Pizzas = new List<Pizza>
            {
                new Pizza { Id = 1, Name = "Classic Italian", IsGlutenFree = false },
                new Pizza { Id = 2, Name = "Veggie", IsGlutenFree = true }
            };
        }

        public static List<Pizza> GetAll() => Pizzas;

        public static Pizza Get(int id) => Pizzas.FirstOrDefault(p => p.Id == id);

        public static void Add(Pizza pizza)
        {
            pizza.Id = nextId++;
            Pizzas.Add(pizza);
        }

        public static void Delete(int id)
        {
            var pizza = Get(id);
            if(pizza is null)
                return;

            Pizzas.Remove(pizza);
        }

        public static void Update(Pizza pizza)
        {
            var index = Pizzas.FindIndex(p => p.Id == pizza.Id);
            if(index == -1)
                return;

            Pizzas[index] = pizza;
        }
    }
}
           

5.建立控制器controllers/ContosoPizza

using System.Collections.Generic;
using System.Linq;
using ContosoPizza.Models;
using ContosoPizza.Services;
using Microsoft.AspNetCore.Mvc;

namespace ContosoPizza.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class PizzaController : ControllerBase
    {
        // GET all action
        [HttpGet]
        public ActionResult<List<Pizza>> GetAll() => PizzaService.GetAll();

        // GET by Id action
        [HttpGet("{id}")]
        //route模式:uri/pizza/3 .

        public ActionResult<Pizza> Get(int id)
        {
            var pizza = PizzaService.Get(id);

            if (pizza == null) return NotFound();

            return pizza;
        }

        // POST action,注意,傳回的是iaction,用CreatedAtAction來建立,不是直接傳回對象
        [HttpPost]
        public IActionResult Create(Pizza pizza)
        {
            PizzaService.Add (pizza);
            return CreatedAtAction(nameof(Create),
            new { id = pizza.Id },
            pizza);
        }

        [HttpPut("{id}")]
        public IActionResult Update(int id, Pizza pizza)
        {
            if (id != pizza.Id) return BadRequest();

            var existingPizza = PizzaService.Get(id);
            if (existingPizza is null) return NotFound();

            PizzaService.Update (pizza);

            return NoContent();
        }

        [HttpDelete("{id}")]
        public IActionResult Delete(int id)
        {
            var pizza = PizzaService.Get(id);

            if (pizza is null) return NotFound();

            PizzaService.Delete (id);

            return NoContent();
        }
    }
}


           

繼續閱讀