這裏介紹在ASP.NET Core中使用Web API建立 RESTful 服務,本文使用VSCode + NET Core3.0html
建立簡單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) }); } } }
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); } }
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" }, ]
簡單介紹一下,api
文件後綴是http 或 rest瀏覽器
定義全局變量:@baseUrl = https://localhost:5001 ,注意連接不加引號app
### 分割多個請求dom
POST/PUT 請求緊跟Head請求信息,換行加上請求內容
格式化輸出
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; }
Url | 響應 |
---|---|
GET {{baseUrl}}/Order/{{orderId}} HTTP/1.1
|
JSON(若配置格式化輸出) |
GET {{baseUrl}}/Order/{{orderId}}.xml
|
XML(若配置格式化輸出) |
GET {{baseUrl}}/Order/{{orderId}}.json
|
JSON(若配置格式化輸出) |
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; }); }
PUT 和 PATCH 方法用於更新現有資源。 它們之間的區別是,PUT 會替換整個資源,而PATCH 僅指定更改。
什麼是JSON Patch?
JSON Patch官網 裏面有一句介紹的很清楚:JSON Patch is a format for describing changes to a JSON document. (一種描述Json的變化的格式)
何時須要用到JSON Patch
ASPNET Core如何處理JSON Patch 請求
1. 添加包支持
dotnet add package Microsoft.AspNetCore.JsonPatch
2. 使用 HttpPatch 屬性進行批註
3. 接受 JsonPatchDocument<T>,一般帶有 [FromBody]
4. 調用 ApplyTo 以應用更改
假設咱們如今有一個完成訂單的需求
下面附上代碼和提交的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); }
失敗的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" }, ]
會在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" }, ]
咱們用Get請求從新查一下,能夠看到IsComplete成功被修改了
這裏只是簡單介紹JSON Patch使用,更多使用方法參考JSON Pan官網 和 微軟文檔
Api 一般須要跟客戶端,前端進行溝通,須要編寫文檔,這須要花費大量時間。
Open Api是專門解決這種問題,它爲RESTful api定義了一個標準的、與語言無關的接口,利用工具生成文檔,能夠作到代碼即文檔(逼着開發者完善註釋)
ASPNET Core 可使用Swashbuckle.AspNetCore或NSwag 生成Swagger 文檔
下面介紹如何使用Swashbuckle.AspNetCore
1、使用Swashbuckle.AspNetCore
dotnet add package Swashbuckle.AspNetCore
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註釋
加上上面生成文檔後,未註釋的函數,屬性會發出警告,警告代碼1591,忽略警告能夠添加多個,分號分割
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); });
/// <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>
ProducesResponseType 描述返回類型
remarks 會生成請求說明
Web Api 使用就介紹這些,若有錯漏,但願指出。
轉發請標明出處:https://www.cnblogs.com/WilsonPan/p/11945856.html
示例代碼:https://github.com/WilsonPan/AspNetCoreExamples/tree/master/WebApi