iT邦幫忙

0

Day 11|ASP.NET Core 路由與 Controller

c#
  • 分享至 

  • xImage
  •  

今日學習目標

  • 理解 API 路由的運作方式
  • 了解 Minimal API 與 Controller 的差別
  • 建立 TasksController.cs
  • 完成 GET 取得所有任務與單筆任務
  • 學會使用 HTTP 狀態碼 200、404

什麼是 Routing(路由)

路由就是告訴後端當使用者發送請求到某個網址時,應該執行哪一段程式
https://ithelp.ithome.com.tw/upload/images/20261017/201844309ZIntEZEFI.png

Minimal API 與 Controller 有什麼不同

Minimal API的好處是簡單、程式碼少,適合小型 API。
Controller 則是把相關 API 集中到一個類別中

建立 TasksController

https://ithelp.ithome.com.tw/upload/images/20261017/20184430IkxKPwIIFC.png

建立 Controllers 資料夾

https://ithelp.ithome.com.tw/upload/images/20261017/20184430XHfsmZbdzd.png

撰寫 TasksController

https://ithelp.ithome.com.tw/upload/images/20261017/201844305M8ymnFzxK.pnghttps://ithelp.ithome.com.tw/upload/images/20261017/201844301XcHJrcoIK.png
結果
https://ithelp.ithome.com.tw/upload/images/20261017/20184430qcZcfQIpLa.png


*提醒邦友,使用第三方服務/API 時,請務必評估資安風險與隱私保護
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言