【ASP.NET Core學習】Web API

 這裏介紹在ASP.NET Core中使用Web API建立 RESTful 服務,本文使用VSCode + NET Core3.0html

  1. 建立簡單Rest API
  2. 格式化輸出
  3. JSON Patch請求
  4. Open API(Swagger)集成

建立簡單Rest API前端

在終端輸入git

dotnet new webapi -n WebAPI

1. 建立Order模型,而後初始化數據github

public class OrderStore
{
    public List<Order> Orders { get; } = new List<Order>();

    public OrderStore()
    {
        var random = new Random();
        foreach (var item in Enumerable.Range(1, 10))
        {
            Orders.Add(new Order
            {
                Id = item,
                OrderNo = DateTime.Now.AddSeconds(random.Next(100, 200)).AddMilliseconds(random.Next(20, 50)).Ticks.ToString(),
                Quantity = random.Next(1, 10),
                Amount = Math.Round(((decimal)random.Next(100, 500) / random.Next(2, 6)), 2)
            });
        }
    }
}
View Code

2. 簡單REST API接口web

/// <summary>
/// 訂單模塊
/// </summary>
[ApiController]
[Route("[controller]")]
[FormatFilter]
public class OrderController : ControllerBase
{

    readonly Models.OrderStore _orderStore = null;
    public OrderController(Models.OrderStore orderStore)
    {
        _orderStore = orderStore;
    }

    /// <summary>
    /// 查詢全部訂單
    /// </summary>
    [HttpGet]
    public ActionResult<List<Models.Order>> GetAll() => _orderStore.Orders;

    /// <summary>
    /// 獲取訂單    
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id:int}.{format?}")]
    public ActionResult<Models.Order> GetById(int id)
    {
        var order = _orderStore.Orders.FirstOrDefault(m => m.Id == id);

        if (order == null)
        {
            return NotFound();
        }

        return order;
    }

    /// <summary>
    /// 建立訂單
    /// </summary>
    /// <param name="order"></param>
    /// <returns>成功返回訂單Id,失敗返回-1</returns>
    [HttpPost]
    public ActionResult<int> Create(Models.Order order)
    {
        if (_orderStore.Orders.Any(m => m.OrderNo == order.OrderNo))
        {
            return -1;
        }

        order.Id = _orderStore.Orders.Max(m => m.Id) + 1;
        _orderStore.Orders.Add(order);

        return order.Id;
    }

    /// <summary>
    /// 更新訂單
    /// </summary>
    /// <returns></returns>
    [HttpPut]
    public ActionResult<bool> Update(Models.Order model)
    {
        Console.WriteLine($"OrderNo:{model.OrderNo}");
        var order = _orderStore.Orders.FirstOrDefault(m => m.OrderNo == model.OrderNo);

        if (order == null)
        {
            return NotFound();
        }

        order.Amount = model.Amount;
        order.Quantity = model.Quantity;

        return true;
    }

    /// <summary>
    /// 更新訂單指定信息
    /// </summary>
    /// <remarks>
    /// Sample request:
    /// 
    ///     PATCH  /Order/{orderNo} 
    ///     [
    ///         {
    ///             "op": "test",
    ///             "path": "/quantity",
    ///             "value": "2"
    ///         },
    ///         {
    ///             "op": "test",
    ///             "path": "/amount",
    ///             "value": "38.28"
    ///         },
    ///         {
    ///             "op": "add",
    ///             "path": "/isComplete",
    ///             "value": "true"
    ///         },
    ///     ]
    /// </remarks>
    /// <returns>返回是否成功</returns>
    /// <response code="200">提交成功</response>
    /// <response code="400">提交參數異常</response>    
    /// <response code="404">訂單號不存在</response>
    [HttpPatch("{orderNo:length(18)}")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public ActionResult<bool> Update([FromBody] JsonPatchDocument<Models.Order> patchDoc, [FromRoute] string orderNo)
    {
        var order = _orderStore.Orders.FirstOrDefault(m => m.OrderNo == orderNo);

        if (order == null)
        {
            return NotFound();
        }

        patchDoc.ApplyTo(order, ModelState);

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        return Ok(true);
    }
}
View Code

3. 推薦一個VS Code插件(REST Client)測試接口,官方介紹json

@baseUrl = https://localhost:5001

###
GET {{baseUrl}}/Order HTTP/1.1

### 
# @name order
POST {{baseUrl}}/Order HTTP/1.1
Accept: application/json
Content-Type: application/json

{
    "OrderNo": "637109312996909246",
    "Quantity": 2,
    "Amount": 38.28
}

### 

@orderId = {{order.response.body.*}}
GET {{baseUrl}}/Order/{{orderId}}.json HTTP/1.1

### 
GET {{baseUrl}}/Order/{{orderId}}.xml HTTP/1.1
###
GET {{baseUrl}}/Order/{{orderId}} HTTP/1.1
###

PUT {{baseUrl}}/Order HTTP/1.1
Content-Type: application/json
Accept: application/json

{
    "Id": 12,
    "OrderNo": "2019112235759329",
    "Quantity": 2,
    "Amount": 38.28
}

###

GET {{baseUrl}}/Order/11

###

PATCH  {{baseUrl}}/Order/637109312996909246 HTTP/1.1
Accept: application/json
Content-Type: application/json

[
  {
    "op": "test",
    "path": "/quantity",
    "value": "2"
  },
  {
    "op": "test",
    "path": "/amount",
    "value": "38.28"
  },
  {
    "op": "add",
    "path": "/isComplete",
    "value": "true"
  },
]




Sample request:

PATCH  /Order/{orderNo} 

[
  {
    "op": "test",
    "path": "/quantity",
    "value": "2"
  },
  {
    "op": "test",
    "path": "/amount",
    "value": "38.28"
  },
  {
    "op": "add",
    "path": "/isComplete",
    "value": "true"
  },
]
View Code

簡單介紹一下,api

文件後綴是http 或 rest瀏覽器

定義全局變量:@baseUrl = https://localhost:5001   ,注意連接不加引號app

### 分割多個請求dom

POST/PUT 請求緊跟Head請求信息,換行加上請求內容

Ctrl + Alt + R 快捷鍵 / 點Send Request發起請求
 

格式化輸出

Api接口一般會是不一樣客戶端調用,這樣會有可能出現須要不一樣響應格式,例如經常使用的Json,XML。
ASPNET Core 默認狀況下是忽略 Accept 標頭,JSON格式返回
1、支持XML格式
1. 添加xml格式化
services.AddControllers(options =>
    {
        options.RespectBrowserAcceptHeader = true;  //接受瀏覽器標頭
    })
    .AddXmlSerializerFormatters();                   //添加XMl格式化
}

 2. 請求是添加標頭

@orderId = {{order.response.body.*}}
GET {{baseUrl}}/Order/{{orderId}} HTTP/1.1
Accept: text/xml

 若不添加標頭,默認使用JSON格式輸出

 

2、URL格式映射

1. 添加[FormatFilter]過濾器,它會檢查路由中格式是否存在,而且使用相應的格式化程序輸出

2. 路由規則添加{format?}

[HttpGet("{id:int}.{format?}")]
public ActionResult<Models.Order> GetById(int id)
{
    var order = _orderStore.Orders.FirstOrDefault(m => m.Id == id);

    if (order == null)
    {
        return NotFound();
    }

    return order;
}
View Code

 

Url 響應
GET {{baseUrl}}/Order/{{orderId}} HTTP/1.1
JSON(若配置格式化輸出)
GET {{baseUrl}}/Order/{{orderId}}.xml
XML(若配置格式化輸出)
GET {{baseUrl}}/Order/{{orderId}}.json
JSON(若配置格式化輸出)
 
3、添加基於 Newtonsoft.Json 的 JSON 格式支持
 
在ASPNET Core 3.0開始,再也不使用Newtonsoft.Json格式化JSON,而是使用System.Text.Json格式化,咱們能夠替換成Newtonsoft.Json
 
1. 添加包
dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson

 2. 配置Newtonsoft.Json

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers()
        .AddNewtonsoftJson(options =>                   //添加基於NewtonsoftJson格式化
        {
            options.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
            options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
        });
}
 
JSON Patch請求

PUT 和 PATCH 方法用於更新現有資源。 它們之間的區別是,PUT 會替換整個資源,而PATCH 僅指定更改。

什麼是JSON Patch?

JSON Patch官網 裏面有一句介紹的很清楚:JSON Patch is a format for describing changes to a JSON document. (一種描述Json的變化的格式)

何時須要用到JSON Patch

  1. 咱們返回的JSON很大,修改可能只是某些字段
  2. 對性能要求比較大的地方
  3. 一個大的對象,好幾個地方修改,而後統一接口修改

ASPNET Core如何處理JSON Patch 請求

1. 添加包支持

dotnet add package Microsoft.AspNetCore.JsonPatch

2. 使用 HttpPatch 屬性進行批註

3. 接受 JsonPatchDocument<T>,一般帶有 [FromBody]

4. 調用 ApplyTo 以應用更改

假設咱們如今有一個完成訂單的需求

  1. 檢查金額,數量是否有變動
  2. 更新IsComplete = true

下面附上代碼和提交的JSON

控制器代碼

[HttpPatch("{orderNo:length(18)}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ActionResult<bool> Update([FromBody] JsonPatchDocument<Models.Order> patchDoc, [FromRoute] string orderNo)
{
    var order = _orderStore.Orders.FirstOrDefault(m => m.OrderNo == orderNo);

    if (order == null)
    {
        return NotFound();
    }

    patchDoc.ApplyTo(order, ModelState);

    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    return Ok(true);
}
View Code

失敗的JSON(金額校驗不過)

PATCH  {{baseUrl}}/Order/637109312996909246 HTTP/1.1
Accept: application/json
Content-Type: application/json

[
  {
    "op": "test",
    "path": "/quantity",
    "value": "2"
  },
  {
    "op": "test",
    "path": "/amount",
    "value": "38.28"
  },
  {
    "op": "add",
    "path": "/isComplete",
    "value": "true"
  },
]
View Code

 

會在ModelState裏面列出校驗不過的信息

 成功的JSON

PATCH  {{baseUrl}}/Order/637109312996909246 HTTP/1.1
Accept: application/json
Content-Type: application/json

[
  {
    "op": "test",
    "path": "/quantity",
    "value": "2"
  },
  {
    "op": "test",
    "path": "/amount",
    "value": "36.8"
  },
  {
    "op": "add",
    "path": "/isComplete",
    "value": "true"
  },
]
View Code

 

咱們用Get請求從新查一下,能夠看到IsComplete成功被修改了

這裏只是簡單介紹JSON Patch使用,更多使用方法參考JSON Pan官網微軟文檔

 

Open API(Swagger)集成

Api 一般須要跟客戶端,前端進行溝通,須要編寫文檔,這須要花費大量時間。

Open Api是專門解決這種問題,它爲RESTful api定義了一個標準的、與語言無關的接口,利用工具生成文檔,能夠作到代碼即文檔(逼着開發者完善註釋)

ASPNET Core 可使用Swashbuckle.AspNetCoreNSwag 生成Swagger 文檔

下面介紹如何使用Swashbuckle.AspNetCore

1、使用Swashbuckle.AspNetCore

  1. 安裝Swashbuckle.AspNetCore包

    dotnet add package Swashbuckle.AspNetCore
  2. 添加並配置 Swagger 中間件

    引用命名空間:using Microsoft.OpenApi.Models;
    services.AddSingleton<Models.OrderStore>();
                            
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "Web Api Doc", Version = "v1" });
    });
    app.UseSwagger();
                                    
    app.UseSwaggerUI(c =>
    {
      c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    });

     通過上面兩步就可使用SwaggerUI來查看文檔和測試,瀏覽器打開(http://{url}/swagger)

2、添加XML註釋

上面生成的Swagger文檔是不包含XML註釋,下面介紹如何添加XML註釋

  1. 項目文件(*.csproj)添加如下

    <PropertyGroup>
        <GenerateDocumentationFile>true</GenerateDocumentationFile>
        <NoWarn>$(NoWarn);1591</NoWarn>
    </PropertyGroup>

     加上上面生成文檔後,未註釋的函數,屬性會發出警告,警告代碼1591,忽略警告能夠添加多個,分號分割

  2. AddSwaggerGen添加下面XML支持

    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "Web Api Doc", Version = "v1" });
    
        var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
        c.IncludeXmlComments(xmlPath);
    });
  3. 方法添加註釋

    /// <summary>
    /// 更新訂單指定信息
    /// </summary>
    /// <remarks>
    /// Sample request:
    /// 
    ///     PATCH  /Order/{orderNo} 
    ///     [
    ///         {
    ///             "op": "test",
    ///             "path": "/quantity",
    ///             "value": "2"
    ///         },
    ///         {
    ///             "op": "test",
    ///             "path": "/amount",
    ///             "value": "38.28"
    ///         },
    ///         {
    ///             "op": "add",
    ///             "path": "/isComplete",
    ///             "value": "true"
    ///         },
    ///     ]
    /// </remarks>
    /// <returns>返回是否成功</returns>
    /// <response code="200">提交成功</response>
    /// <response code="400">提交參數異常</response>    
    /// <response code="404">訂單號不存在</response>
    View Code

    ProducesResponseType 描述返回類型

    remarks 會生成請求說明

  4. 效果

Web Api 使用就介紹這些,若有錯漏,但願指出。

相關文章
相關標籤/搜索