ASP.NET Core 3 高级编程(第8版) 学习笔记 04

第 19 章主要介绍 Restful Service 的相关知识。Restful Service 的核心内容是:(1)HTTP 请求或 HTTP 动词,用 HTTP 请求表达不同的操作,最好遵守惯例。(2)资源,通过 PATH 结合 payload 来表达。以本章的示例来说:

请添加图片描述

除 Restful Service 外,替代品包括 GraphQL 和 gRPC。

使用自定义的端点实现 Web Service

不同语言的 Web 框架,都需要解决 Routing 问题,也就是客户端提交的请求,服务器通过内部机制最终处理的方法或者函数。在 asp.net core 中,比较流程的方法就是实用 MVC 来实现路由。但在使用标准的 MVC 框架之前,本书介绍了通过自定义 endpoint 来实现的方法。结合第 18 章,也就是说解决路由问题可以有 3 种方法:
1)使用中间件
2)自定义 endpoint
3)使用 MVC 框架的 Controller

第一步:新建 WebserviceEndpoint 类,在该类中实现了获取所有 products,根据 id 获取 product 和创建一个新的 product 三个功能:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using System.Collections;
using System.Collections.Generic;
using System.Text.Json;
using WebApp.Models;

namespace WebApp
{
    public static class WebServiceEndpoint
    {
        private static string BASE_URL = "api/products";

        public static void MapWebService(this IEndpointRouteBuilder app)
        {
            // 匹配 GET: /api/products/{id}
            app.MapGet($"{BASE_URL}/{{id}}", async context =>
            {
                long key = long.Parse(context.Request.RouteValues["id"] as string);
                DataContext dbContext = context.RequestServices.GetService<DataContext>();

                var p = dbContext.Products.Find(key);
                if (p == null) {
                    context.Response.StatusCode = StatusCodes.Status404NotFound;
                } else {
                    context.Response.ContentType = "application/json";
                    context.Response.StatusCode = StatusCodes.Status200OK;
                    await context.Response.WriteAsync(JsonSerializer.Serialize<Product>(p));
                }
            });

            app.MapGet(BASE_URL, async context => 
            {
                DataContext dbContext = context.RequestServices.GetService<DataContext>();
                context.Response.ContentType = "application/json";

                await context.Response.WriteAsync(
                    JsonSerializer.Serialize<IEnumerable<Product>>(dbContext.Products));
            });

            app.MapPost(BASE_URL, async context => 
            {
                DataContext dbContext = context.RequestServices.GetService<DataContext>();
                Product p = await JsonSerializer.DeserializeAsync<Product>(context.Request.Body);
                await dbContext.AddAsync(p);
                await dbContext.SaveChangesAsync();

                context.Response.StatusCode=StatusCodes.Status200OK;
            });
        }
    }
}

注意 MapService() 方法是 IEndpointRouteBuilder 的扩展方法

第二步:在 Startup 类的 Configure() 方法中注册服务:

测试自定义端点

测试建议使用流行的工具,比如 Postman,而不是使用书中所介绍的 Invoke-ReestMethod 等工具。打开 Postman,对自定义 endpoint 的 3 个功能进行测试:

提交后,在数据库中查看:

使用控制器创建 Web 服务

第一步:在 Startup 类的 ConfigureService() 方法中,将控制器的服务添加到指定的 IServiceCollection,并且在 Configure() 中,通过 endpoints.MapControllers(); 方法实现默认的路由。

第二步:创建 Controller ,定义 Action 方法。XXXController 从 ControllerBase 类派生

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using WebApp.Models;

namespace WebApp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductsController : ControllerBase
    {
        private DataContext dbContext;

        public ProductsController(DataContext dbContext)
        {
            this.dbContext = dbContext;
        }

        // Get products
        [HttpGet]
        public IEnumerable<Product> GetProducts()
        {
            return dbContext.Products;
        }

        // Get product by Id
        [HttpGet("{id}")]
        public Product GetProduct()
        {
            return dbContext.Products.FirstOrDefault();
        }

        // Create product
        [HttpPost]
        public void SaveProduct([FromBody]Product p)
        {
            dbContext.Products.Add(p);
            dbContext.SaveChanges();
        }
    }
}

Controller 实现了相同的功能,我们可以从中看到框架的好处。

源码

pro asp.net core 3 notes: 《ASP.NET Core 3高级编程(第8版)》学习笔记

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-24 09:10:05       4 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-24 09:10:05       5 阅读
  3. 在Django里面运行非项目文件

    2024-04-24 09:10:05       4 阅读
  4. Python语言-面向对象

    2024-04-24 09:10:05       4 阅读

热门阅读

  1. 146. LRU 缓存

    2024-04-24 09:10:05       19 阅读
  2. windows驱动开发-I/O请求(二)

    2024-04-24 09:10:05       19 阅读
  3. Linux内核驱动开发-006内核定时器

    2024-04-24 09:10:05       17 阅读
  4. c#版本LabelMe标注辅助工具源码

    2024-04-24 09:10:05       17 阅读
  5. [SP10606] BALNUM - Balanced Numbers 解题记录

    2024-04-24 09:10:05       21 阅读
  6. 算法题解记录21+++打家劫舍(百日筑基)

    2024-04-24 09:10:05       19 阅读
  7. kafak知识总结(2)

    2024-04-24 09:10:05       17 阅读
  8. 对React-Fiber的理解,它解决了什么问题?

    2024-04-24 09:10:05       19 阅读
  9. 网站安全方案

    2024-04-24 09:10:05       16 阅读
  10. 阿里云域名动态解析

    2024-04-24 09:10:05       17 阅读
  11. 使用python写一个井字棋窗口小游戏

    2024-04-24 09:10:05       20 阅读