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 三个功能:

cs 复制代码
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 类派生。

cs 复制代码
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版)》学习笔记

相关推荐
flying robot11 分钟前
小结:网页性能优化
笔记
tcoding25 分钟前
《Elasticsearch 源码解析与优化实战》笔记
笔记
头疼的程序员32 分钟前
docker学习与使用(概念、镜像、容器、数据卷、dockerfile等)
学习·docker·容器
TJ-周月年1 小时前
2025第三届盘古初赛(计算机部分)
笔记
LVerrrr2 小时前
Missashe考研日记—Day37-Day43
学习·考研
TIF星空4 小时前
【使用 C# 获取 USB 设备信息及进行通信】
开发语言·经验分享·笔记·学习·microsoft·c#
AI算法工程师Moxi6 小时前
什么时候可以开始学习深度学习?
人工智能·深度学习·学习
jiedaodezhuti8 小时前
ElasticSearch重启之后shard未分配问题的解决
笔记·elasticsearch
z人间防沉迷k9 小时前
堆(Heap)
开发语言·数据结构·笔记·python·算法
z542968z9 小时前
Springboot3自定义starter笔记
笔记