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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值