前言
建立 ABP MVC Web 專案後,
執行 Web 專案時,會先到 Web 首頁,
輸入 /swagger
後,會顯示 Swagger UI。
在 Swagger UI 中,可以透過 Try it out 來直接測試 Web API。
當輸入資料,按下 Execute 後,結果卻是出現 400 Error: Bad Request
連 Method 的程式碼都沒有執行,如下圖,

解決
查看一下 Console 輸出有出現 The required antiforgery header value "RequestVerificationToken" is not present.
的警告,
然後最後就給 400。

因為它需要有 antiforgery header,
開啟開發者工具,
在 Cookies 中有一個 .AspNetCore.Antiforgery.oSFhvIVq2CY
的 Cookie,如下,

它應該是先前在 UI 中產生的 Cookie ,
所以當直接在 URL 中直接輸入 /swagger
時,
Cookie 並不會被清除,所以在 API 執行時,
會進行 Antiforgery 的檢查,於是就噴 400 的錯了。
將該 Cookie 清除,再執行一次,就正常了。

下次如果透過 Swagger 執行 API,連程式都沒進去就噴400時,
請檢查一下 Log 及 Cookie 是否是因為 Antiforgery 的原因哦~~