Bolger
.NET
C#
Blazor
Github pages
WebAssembly
隨著前端技術日新月異,微軟自己也推出了前端框架(Blazor),如果 Blazor WebAssembly 是純前端網站,我就在思考是否可以將它,部屬到 GitHub Pages 上執行呢? 透過找尋幾位大神的部落格,發現真的可以將 Blazor WebAssembly 部屬到 GitHub Pages 上,只是需要注意一些小細節,此篇就是我記錄操作流程。
# 建立 BlazorGitHubPages 目錄
mkdir BlazorGitHubPages
cd BlazorGitHubPages
# 建立 Blazor WebAssembly App 專案
dotnet new blazorwasm
# 使用 VSCode 開啟專案
code .
dotnet run
測試一下,專案是否能正常執行dotnet run
# 正在建置...
# info: Microsoft.Hosting.Lifetime[14]
# Now listening on: https://localhost:7019
# info: Microsoft.Hosting.Lifetime[14]
# Now listening on: http://localhost:5089
# info: Microsoft.Hosting.Lifetime[0]
# Application started. Press Ctrl+C to shut down.
# info: Microsoft.Hosting.Lifetime[0]
# Hosting environment: Development
# info: Microsoft.Hosting.Lifetime[0]
# Content root path: C:\Users\Grape\BlazorGitHubPages
# info: Microsoft.Hosting.Lifetime[0]
# Application is shutting down...
gitignore
,初始化 git repositry,並提交版本。# 建立 gitignore
dotnet new gitignore
# 建立 git repositry
git init
# 將所有差異加入索引
git add --all
# 提交版本
git commit -m "init"
# 新增 remote (需要改成自己 remote)
git remote add origin https://github.com/[USERNAME]/[REPOSITORY_NAME].git
# Push to GitHub
git push -u origin master
如果按照上面指令執行後,便可以在 GitHub 上看見檔案被推上去。
.net
來找 .NET 範本。name: .NET
on:
push:
branches: [ master ]
jobs:
deploy-to-github-pages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: 6.0.x
# 修改成專案名稱
- name: Publish .NET Core Project
run: dotnet publish [REPOSITORY_NAME].csproj -c Release -o release --nologo
- name: Commit wwwroot to GitHub Pages
uses: JamesIves/github-pages-deploy-action@v4.3.3
with:
BRANCH: gh-pages
FOLDER: release/wwwroot
fatal: unable to access 'xxx': The requested URL returned error: 403
如果在執行 Actions 執行時,可能會遇到 403 問題,哪就表示 workflow 權限不足,需要開啟權限,開啟方式如下:
Failed to load resource: the server responded with a status of 404 ()
為什麼會產生此問題呢? 因為部屬上去 GitHub Pages 預設路徑為 [USERNAME].github.io/[REPOSITORY_NAME],所以必須修改 index.html 裡面 base href。
<!-- 修改成 Repository name -->
<base href="/[REPOSITORY_NAME]/" />
# 修改 index.html 的 base href 設定
- name: 修改 index.html 的 base href 設定
run: sed -i 's/<base href="\/" \/>/<base href="\/[REPOSITORY_NAME]\/" \/>/g' release/wwwroot/index.html
GET https://oneheed.github.io/BlazorGitHubPage/_framework/blazor.webassembly.js net::ERR_ABORTED 404
產生此問題的原因,因為 GitHub Pages 部屬時,使用 Jekyll 來處理網站,它會自動忽略底線開頭的資料夾,所以造成網站會找不到檔案,故需要告訴它不要忽略檔案。
.nojekyll
檔案 # 新增 .nojekyll 檔
- name: 新增 .nojekyll 檔
run: touch release/wwwroot/.nojekyll
如果找不到頁面 GitHub Pages 會預設自己頁面,這個並非我們想要的,故可以將 index.html 複製成 404.html,如果找不到就會顯示 "Sorry, there's nothing at this address."。
# 複製 index.html 成 404.html
- name: 複製 index.html 成 404.html
run: cp release/wwwroot/index.html release/wwwroot/404.html
經歷以上的問題的處理,終於成功將網站部屬上去並可以正常操作,如果有遇到上述沒有提到問題歡迎一起討論
最後 YML 檔
name: .NET
on:
push:
branches: [ master ]
jobs:
deploy-to-github-pages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: 6.0.x
- name: Publish .NET Core Project
run: dotnet publish BlazorGitHubPages.csproj -c Release -o release --nologo
# 修改 index.html 的 base href 設定
- name: 修改 index.html 的 base href 設定
run: sed -i 's/<base href="\/" \/>/<base href="\/BlazorGitHubPage\/" \/>/g' release/wwwroot/index.html
# 新增 .nojekyll 檔
- name: 新增 .nojekyll 檔
run: touch release/wwwroot/.nojekyll
# 複製 index.html 成 404.html
- name: 複製 index.html 成 404.html
run: cp release/wwwroot/index.html release/wwwroot/404.html
# Commit wwwroot to GitHub Pages
- name: Commit wwwroot to GitHub Pages
uses: JamesIves/github-pages-deploy-action@v4.3.3
with:
branch: gh-pages
folder: release/wwwroot
最後完成 GitHub 網站
https://oneheed.github.io/BlazorGitHubPage/
手把手將 Blazor WebAssembly 部署到 GitHub Pages
Github Pages 裝載和部署 ASP.NET Core Blazor WebAssembly
裝載和部署 ASP.NET Core Blazor WebAssembly
How to deploy ASP.NET Blazor WebAssembly to GitHub Pages
How to deploy Blazor WebAssembly as static site in GitLab Pages
Continuous integration and Continuous deployment of Blazor application to GitLab page.