- 取得連結
- 以電子郵件傳送
- 其他應用程式
此文章為記錄在 Linux Ubuntu 作業系統執行 Visual Studio 2015 發行的 ASP.NET Core WebAPI 應用程式,與執行 .NET Core 主控台應用程式差不多。
預設應用程式的路徑為「http://localhost:5000」,若要透過其他電腦的瀏覽器連線的話需要修改 Program.cs 檔案的設定。
文章環境
- Linux Ubuntu 16.0.4.1 LTS
- .NET Core version 1.0.0-preview2-1-003177
- Microsoft Visual Studio 2015 Community 2015 Update 3
- Microsoft .NET Core Tools (Preview 2) 14.1.21111.0
在 Visual Studio 發行 ASP.NET Core Web Application
開啟 Visual Studio 建立一個 ASP.NET Core Web Application,選擇 Web API 樣板。
![建立 ASP.NET Core 應用程式](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/001-create-new-aspnet-core-app.png)
![選擇應用程式類型](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/002-select-new-aspnet-core-app-detail.png)
修改程式碼內容。
![修改預設程式碼內容](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/003-modify-default-valuescontroller-code.png)
預設應用程式的路徑為 http://localhost:5000,因要透過其他電腦瀏覽器連線,要修改 Program.cs 設定變更網址為「http://*:5000」,接受遠端來源連線。
![變更預設監聽設定](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/004-modify-default-url-setting.png)
發行 WebAPI 應用程式。
![專案中點選右鍵進行應用程式發佈](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/005-deploy-aspnet-core-app.png)
![選擇自訂發佈設定](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/006-select-target-to-custom.png)
![輸入發佈設定檔名](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/007-insert-setting-name.png)
![選擇發佈儲存的位置](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/008-set-deploy-folder.png)
![確認發佈位置](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/009-finish-deploy-location-setting-click-next.png)
![進行進階設定](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/010-setting-value-when-ever-you-want.png)
![點選發行按鈕開始發佈](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/011-click-deploy-to-publish-app.png)
發行完成後會在輸出視窗中顯示結果。
![發行完成後顯示發佈結果](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/012-publish-success-in-ouput-screen.png)
應用程式發行後的檔案內容(部分)。
![發行後檔案內容](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/013-deploy-site-detail.png)
在 Ubuntu 執行 ASP.NET Core 應用程式
將應用程式發行後的檔案複製到 Ubuntu。
![將發行檔案搬移到要執行的 Ubuntu 電腦](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/014-copy-deploy-file-to-ubuntu.png)
進入應用程式檔案的資料夾,執行應用程式檔案(要執行的檔案名稱預設與專案名稱相同)。
![進入發行檔案的資料夾](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/015-run-webapi-in-ubuntu.png)
![要執行的檔案](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/016-check-dll-file.png)
使用「dotnet <要執行檔案名稱>」啟動 ASP.NET Core WebAPI 應用程式。
![使用 dotnet 指令啟動應用程式](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/017-execute-dotnet-dll-file.png)
執行完畢後會顯示監聽中的連線位置(此設定為監聽連接埠 5000)。
![執行完畢後顯示連線資訊](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/018-ouput-listening-port-and-execute-result.png)
開啟其他電腦的網際網路瀏覽器輸入位置後取得 API 回傳的 Json 字串內容(呈現伺服器時間)。
![使用網際網路瀏覽器執行應用程式內容](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/run-webapi-in-ubuntu/019-use-client-browser-connect-ubuntu-website.png)
執行方式類似 Self-Host Console Application,官方文件有提到如何掛載成 service 在 Ubuntu 重新啟動後會持續開啟網站,請參考下面列出的參考資料。
留言
張貼留言