• 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;
                });
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    注意 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();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

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

    源码

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

  • 相关阅读:
    Dubbo SpringBoot 实战
    自动驾驶仿真:python和carsim联合仿真案例
    SpringMVC 程序开发
    【UOJ 454】打雪仗(通信题)(分块)
    【图像分类】2022-MaxViT ECCV
    css style、css color 转 UIColor
    asp.net网上销售系统VS开发mysql数据库web结构c#编程Microsoft Visual Studio计算机毕业设计
    《Python编程基础》第1章、简介及示例
    Arduino驱动LM35线性温度传感器(温湿度传感器)
    游戏设计模式专栏(六):在Cocos游戏开发中运用适配器模式
  • 原文地址:https://blog.csdn.net/stone0823/article/details/138015952