.Net WebAPI(一)

发布于:2024-12-18 ⋅ 阅读:(53) ⋅ 点赞:(0)


项目地址

  • 教程作者:
  • 教程地址:
  • 代码仓库地址:
  • 所用到的框架和插件:
webapi

一、WebAPI基础

1. 项目初始化

  1. 创建一个一个项目文件夹,并且在控制台输入
dotnew webapi -n Restaurants.API --noopenapi -controllers
  1. 但是此时的项目是没有sln文件的,创建sln文件,但是这时候打开vs显示的是空项目
dotnet new sln
  1. 将项目添加到vs里
 dotnet sln add ./Restaurants.API

1.1 创建简单的API

  • 在program.cs里使用routing中间件配置路由
1.1.1 get请求
  • 获取所有的shirts
app.MapGet("/shirts", () =>
{
    return "Reading all the shirts";
});
  • 根据ID获取一个
//2.get shirt by id 
app.MapGet("/shirts/{id}", (int id) =>
{
    return $"Reading shirt with ID: {id}";
});
1.1.2 post请求
app.MapPost("/shirts", () =>
{
    return "Creating a new shirt.";
});

1.1.3 put请求

更新

app.MapPut("/shirts/{id}", (int id) =>
{
    return $"Updating shirt with ID: {id}";
});
1.1.4 Delete请求

删除

app.MapDelete("/shirts/{id}", (int id) =>
{
    return $"Deleting shirt with ID: {id}";
});

1.2 webapi的流程

在这里插入图片描述

2.Controllers

2.1 创建一个shirts的Controller

  • 创建Controllers文件夹,在该文件夹下创建ShirtsController.cs文件
using Microsoft.AspNetCore.Mvc;

namespace WebAPIDemo.Controllers
{
    [ApiController]
    public class ShirtsController : ControllerBase
    {
        public string GetShirts()
        {
            return "Reading all the shirts";   
        }

        public string GetShirtById(int id)
        {
            return $"Reading shirt with ID: {id}";
        }

        public string CreateShirt()
        {
            return "Creating a new shirt.";
        }

        public string UpdateShirt() {
            return "Updating shirt with ID: {id}";
        }

        public string DeleteShirt(int id)
        {
            return $"Deleting shirt with ID: {id}";
        }

    }
}

3. Routing

3.1 使用和创建MapControllers

  1. Program.cs里添加rout的中间件

在这里插入图片描述

  1. 在Controller里,直接使用特性标记routing
using Microsoft.AspNetCore.Mvc;

namespace WebAPIDemo.Controllers
{
    [ApiController]
    public class ShirtsController : ControllerBase
    {
        [HttpGet("/shirts")]
        public string GetShirts()
        {
            return "Reading all the shirts";   
        }

        [HttpGet("/shirts/{id}")]
        public string GetShirtById(int id)
        {
            return $"Reading shirt with ID: {id}";
        }

        [HttpPost("/shirts")]
        public string CreateShirt()
        {
            return "Creating a new shirt.";
        }

        [HttpPut("/shirts/{id}")]
        public string UpdateShirt() {
            return "Updating shirt with ID: {id}";
        }

        [HttpDelete("/shirts/{id}")]
        public string DeleteShirt(int id)
        {
            return $"Deleting shirt with ID: {id}";
        }
    }
}

3.2 使用Routing的模板语言

  1. 在上面我们给每个Controller都使用一个路由,这样代码冗余,我们可以使用模板来指定rout;

在这里插入图片描述

  • 这样我们可以直接用过https://localhost:7232/shirts 进行访问shirts就是控制器的名称
  1. 如果我们想通过https://localhost:7232/api/shirts进行访问的话,修改模板routing

在这里插入图片描述

4. Mould Binding

将Http request和 Controller里的 parameter绑定起来
在这里插入图片描述

4.1 指定数据的来源

4.1.1 给Request请求添加
1. FromRoute
  • 指定这个参数必须是从https://localhost:7232/api/shirts/2/red从路由里来,如果不是,则报错
[HttpGet("{id}/{color}")]
public string GetShirtById(int id,[FromRoute]string color)
{
    return $"Reading shirt with ID: {id},color is {color}";
}
2. FromQuery
  • 必须通过QueryString的形式提供:https://localhost:7232/api/shirts/2?color=red
 [HttpGet("{id}/{color}")]
 public string GetShirtById(int id,[FromQuery]string color)
 {
     return $"Reading shirt with ID: {id},color is {color}";
 }
3. FromHeader
  • 必须通过请求头来传递,且Key是Name
[HttpGet("{id}/{color}")]
public string GetShirtById(int id,[FromHeader(Name="color")]string color)
{
    return $"Reading shirt with ID: {id},color is {color}";
}

在这里插入图片描述

4.1.2 给Post请求添加
  • 在webapp里创建一个新的文件夹Models,并且添加一个Shirts,cs
namespace WebAPIDemo.Models
{
    public class Shirt
    {
        public int ShirtId { get; set; }
        public string? Brand { get; set; }   
        public string? Color { get; set; }
        public int Size { get; set; }
        public string? Gender { get; set; }
        public double Price { get; set; }
    }
}
1. FromBody
  • 从请求体里来
[HttpPost]
public string CreateShirt([FromBody]Shirt shirt)
{
    return "Creating a new shirt.";
}

在这里插入图片描述

2. FromForm
  • 通过表格形式传递
[HttpPost]
public string CreateShirt([FromForm]Shirt shirt)
{
    return "Creating a new shirt.";
}

在这里插入图片描述

5. Mould Validation

5.1 添加Model Validation

  • Models/Shirts.cs类里添加验证,如果没有给出必须的参数,请求会报错400
using System.ComponentModel.DataAnnotations;

namespace WebAPIDemo.Models
{
    public class Shirt
    {
        [Required]
        public int ShirtId { get; set; }
        [Required]
        public string? Brand { get; set; }   
        public string? Color { get; set; }
        public int Size { get; set; }
        [Required]
        public string? Gender { get; set; }
        public double Price { get; set; }
    }
}

5.2 添加自定义的Validation

  1. Models文件夹里,添加Validations文件夹,并且添加文件Shirt_EnsureCorrectSizingAttribute.cs
using System.ComponentModel.DataAnnotations;
using WebAPIDemo.Models;

namespace WebApp.Models.Validations
{
    public class Shirt_EnsureCorrectSizingAttribute : ValidationAttribute
    {
        protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
        {
            var shirt = validationContext.ObjectInstance as Shirt;

            if (shirt != null && !string.IsNullOrWhiteSpace(shirt.Gender))
            {
                if (shirt.Gender.Equals("men", StringComparison.OrdinalIgnoreCase) && shirt.Size < 8)
                {
                    return new ValidationResult("For men's shirts, the size has to be greater or equal to 8.");
                }
                else if (shirt.Gender.Equals("women", StringComparison.OrdinalIgnoreCase) && shirt.Size < 6)
                {
                    return new ValidationResult("For women's shirts, the size has to be greater or equal to 6.");
                }
            }

            return ValidationResult.Success;
        }
    }
}
  1. 我们验证的是Size,所以在Model里的Size添加我们自定义的Attribute
[Shirt_EnsureCorrectSizing]
public int Size { get; set; }

6. WebApi Return Types

6.1 直接返回对象

  • 创建一个List,存放所有的Shirt实例,返回一个Shirt类型
    在这里插入图片描述
  • 通过id访问https://localhost:7232/api/shirts/1, 返回一个json
{
	shirtId: 1,
	brand: "My Brand",
	color: "Blue",
	size: 10,
	gender: "Men",
	price: 30
}

6.2 返回多类型

  • 如果返回多类型,就不能指定具体返回的类型,需要返回一个IActionResult
[HttpGet("{id}")]
public IActionResult GetShirtById(int id)
{
    if (id <= 0)
    {
        return BadRequest("Invalid shirt ID");
    }
    var shirt = shirts.FirstOrDefault(x => x.ShirtId == id);
    if( shirt == null)
    {
        return NotFound();
    }

    return Ok(shirt);
}

7. Action filter

  • 使用Action filter进行data validation

7.1 创建filter

  1. 创建Filters文件夹,并且创建Shirt_ValidateShirtId.cs类,并且添加对ShortId的验证
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using WebAPIDemo.Models.Repositories;


namespace WebAPIDemo.Filters
{
    public class Shirt_ValidateShirtIdFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);
            var shirtId = context.ActionArguments["id"] as int?;
            if (shirtId.HasValue)
            {
                if (shirtId.Value <= 0)
                {
                    context.ModelState.AddModelError("ShirtId", "ShirtId is invalid.");
                    var problemDetails = new ValidationProblemDetails(context.ModelState)
                    {
                        Status = StatusCodes.Status400BadRequest
                    };
                    context.Result = new BadRequestObjectResult(problemDetails);
                }
                else if (!ShirtRepository.ShirtExists(shirtId.Value))
                {
                    context.ModelState.AddModelError("ShirtId", "Shirt doesn't exist.");
                    var problemDetails = new ValidationProblemDetails(context.ModelState)
                    {
                        Status = StatusCodes.Status404NotFound
                    };
                    context.Result = new NotFoundObjectResult(problemDetails);
                }
            }
        }
    }
}
  1. 使用添加的filter,对 id进行验证
[HttpGet("{id}")]
[Shirt_ValidateShirtIdFilter]
public IActionResult GetShirtById(int id)
{
    return Ok(ShirtRepository.GetShirtById(id));
}

网站公告

今日签到

点亮在社区的每一天
去签到