ASP.NET 頁面按鈕點擊 → 後台 `Process.Start("cmd.exe", ...)` 啟動 Beyond Compare → **伺服器桌面彈出視窗**,而非用戶瀏覽器;且嘗試用 `` 顯示報告,結果為空白框。
// Controller
public ActionResult GenerateReport() {
// 1. 後台執行比較邏輯 (不啟動 GUI,改用 BC 命令行 /bcomp.com 或庫)
var reportPath = RunComparisonAndSaveReport(); // 返回伺服器路徑
// 2. 複製到 Web 可訪問目錄 (~/App_Data/Reports/ 或 wwwroot/reports/)
var webPath = "/reports/" + Path.GetFileName(reportPath);
System.IO.File.Copy(reportPath, Server.MapPath(webPath), true);
// 3. 返回 JSON 供前端下載
return Json(new { url = webPath, fileName = "report.html" });
}
// 前端 JS
fetch('/Home/GenerateReport')
.then(r => r.json())
.then(d => { window.location.href = d.url; }); // 觸發瀏覽器下載
public ActionResult GetReportContent() {
var html = RunComparisonAndGetHtmlString(); // 不落地文件
return Content(html, "text/html");
}
// 前端
// ❌ 錯誤:啟動 GUI、阻塞、路徑硬編碼
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/c BCompare.exe @script.txt";
p.Start();
p.WaitForExit(); // 會卡死 IIS 工作進程
// ✅ 正確:非阻塞、返回可訪問 URL
public async Task Compare() {
var reportId = Guid.NewGuid().ToString("N");
var reportPath = Path.Combine(_env.WebRootPath, "reports", reportId + ".html");
// 背景任務 (Hangfire / IHostedService / BackgroundService)
_queue.QueueBackgroundWorkItem(async token => {
await RunBCompareScriptAsync(reportPath, token);
});
return Ok(new { reportUrl = $"/reports/{reportId}.html" });
}
**Web 後台 ≠ 桌面應用**。`Process.Start` 啟動 GUI 只在開發機調試時「碰巧可見」。生產環境必須:**後台無界面生成報告 → 存放靜態文件目錄 → 返回 URL 供前端下載/內嵌**。