掌握不依賴 WebMatrix/Visual Studio 發佈功能的手動部署技巧,適用於僅支援 FTP 的主機、現有經典 ASP 網站遷移、自動化部署腳本、CI/CD 流程整合等實務場景。
WebMatrix 發佈功能自動處理:檔案複製、DLL 組件部署、資料庫腳本執行、Web.config 轉換。手動部署需自行確保:
# 使用 MSBuild 建置發佈套件 (Visual Studio Developer Command Prompt)
msbuild MyWebSite.csproj /p:DeployOnBuild=true /p:PublishProfile=FolderProfile /p:Configuration=Release
# 或使用 dotnet CLI (ASP.NET Core 專案)
dotnet publish -c Release -o ./publish
將開發機網站資料夾 (含所有子目錄) 完整複製至遠端主機應用程式根目錄:
關鍵排除項目:
App_Data/*.sdf - **嚴禁直接複製生產資料庫檔案** (見下節)*.pdb - 偵錯符號檔*.user, *.suo - 使用者專屬設定bin/*.xml - XML 文件檔 (非必要)確保遠端主機 bin/ 資料夾包含以下組件 (Visual Studio 建置輸出或 NuGet 套件):
Microsoft.Web.Infrastructure.dll
NuGet.Core.dll
System.Web.Helpers.dll
System.Web.Razor.dll
System.Web.WebPages.Administration.dll
System.Web.WebPages.Deployment.dll
System.Web.WebPages.dll
System.Web.WebPages.Razor.dll
WebMatrix.Data.dll
WebMatrix.WebData.dll
# 如使用 Entity Framework
EntityFramework.dll
EntityFramework.SqlServer.dll
# 如使用 SQL Server Compact
System.Data.SqlServerCe.dll
System.Data.SqlServerCe.Entity.dll
驗證方式: 部署後瀏覽網站,若出現 Could not load file or assembly 錯誤,對照缺失組件補充。
開發機測試資料 (含測試帳號、臨時資料) 若直接複製 .sdf 檔案至生產環境,將:
<!-- Web.Release.config (XDT 轉換範例) -->
<configuration xmlns:xdt="https://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="DefaultConnection"
connectionString="Server=prod-db;Database=MyApp;User Id=webuser;Password=StrongPwd;"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
<system.web>
<compilation debug="false" xdt:Transform="SetAttributes(debug)" />
<customErrors mode="RemoteOnly" defaultRedirect="~/Error" xdt:Transform="Replace" />
</system.web>
</configuration>
就是這樣。祝部署順利!GOOD LUCK!