.

iT邦幫忙

0

簡單示範將ASP.NET Core API部屬到 Microsoft Azure的方法

  • 分享至 

  • xImage
  •  

本文將簡單示範如何使用 Microsoft Azure來部署你的Web API。

大綱

1.專案準備

2.建立Web 應用程式

3.發布專案

4.測試API 功能

實作開始

專案準備

準備好你的Web API專案。示範專案
https://ithelp.ithome.com.tw/upload/images/20250325/20172416vbG7FUlFWo.png

建立Web 應用程式

1.選擇建立Web應用程式
https://ithelp.ithome.com.tw/upload/images/20250325/20172416EFH9TsIfrs.png
2.輸入名稱要自己記得,執行階段堆疊記得選對架構,這邊選專案使用的版本.NET8
https://ithelp.ithome.com.tw/upload/images/20250325/201724163O5W3Jwqpq.png
3.其餘設定請依照自己需求調整,等待部署成功後就可以了!
https://ithelp.ithome.com.tw/upload/images/20250325/20172416sAlQwCB1KF.png

發布專案

1.前往Visual Studio打開你的專案,對專案點擊右鍵選擇發布
https://ithelp.ithome.com.tw/upload/images/20250325/20172416i0GfiIavY6.png
2.選擇Azure
https://ithelp.ithome.com.tw/upload/images/20250325/20172416geQac2krEN.png
3.選擇App service (Windows) ,依照環境決定,在Azure 建立時選哪個這邊就選哪個
https://ithelp.ithome.com.tw/upload/images/20250325/20172416e9U0bHXeil.png
4.選擇剛剛在Azure建立的Web 應用程式的名稱
https://ithelp.ithome.com.tw/upload/images/20250325/20172416RUe3Utn1qM.png
5.新增API 管理
https://ithelp.ithome.com.tw/upload/images/20250325/201724164DOi1SwolY.png

https://ithelp.ithome.com.tw/upload/images/20250325/20172416l8dkYZOGSz.png
6.設定完成後按發布,等待顯示成功就可以了!
https://ithelp.ithome.com.tw/upload/images/20250325/20172416lea8VXr1ix.png

https://ithelp.ithome.com.tw/upload/images/20250325/20172416oBCfcSNub4.png

https://ithelp.ithome.com.tw/upload/images/20250325/20172416DtHLAQWip5.png

測試API 功能

在下面喔一點可以看到你的網站位置
https://ithelp.ithome.com.tw/upload/images/20250325/20172416U4i2uupyvK.png
點進去顯示找不到不要怕,因為根本就沒有製作頁面,在網址後面輸入/api/[api名稱]就可以進去了或是使用Postman 直接測試API 有沒有運作。
https://ithelp.ithome.com.tw/upload/images/20250325/20172416ynoHWNWUoT.png
看起來是有!
https://ithelp.ithome.com.tw/upload/images/20250325/20172416bBGiWjCqcU.png

在你的網址後面打上/swagger/index.html來開啟swagger

https://ithelp.ithome.com.tw/upload/images/20250325/201724162MfhbMkrLP.png
如果打不開請到專案中的Program.cs

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

改成

app.UseSwagger();
app.UseSwaggerUI();

解除Swagger只會在開發環境使用的限制就可以執行了


尚未有邦友留言

立即登入留言