首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET MVC RC 分離 Controllers/Views 專案後的腳手架完美解決方案(二)

     編輯:關於ASP.NET

問題回顧

專案分離後,Controllers 專案中的 Scaffold 功能創建的 View 文件留在錯誤位置,且「轉到視圖」導航失效。

核心突破:.csproj <Link> 元素

<!-- MyMvc.Controllers.csproj -->
<ItemGroup>
  <Content Include="..\MyMvc.Web\Views\Home\Index.aspx">
    <Link>Views\Home\Index.aspx</Link>
  </Content>
</ItemGroup>

<Link> 在 VS 中表現為「快捷方式」:文件物理位於 Web 專案,但在 Controllers 專案中可見、可雙擊打開、不參與編譯。

終極工具:MvcScaffoldTool 自動化同步

工具界面1 工具界面2

監控邏輯

// FileSystemWatcher 監控 Controllers 專案 Views 目錄
watcher.Created += (s, e) => {
    if (e.FullPath.EndsWith(".aspx") || e.FullPath.EndsWith(".ascx")) {
        // 1. 移動文件到 Web 專案對應目錄
        File.Move(e.FullPath, webProjectPath + relativePath);
        // 2. 更新 Controllers.csproj:移除舊項,添加 Link 項
        UpdateCsprojLink(relativePath);
        // 3. 刷新 VS 專案系統
        DTE.ExecuteCommand("Project.Refresh");
    }
};

效果驗證

  • ✅ 右鍵「添加視圖」→ 文件自動落入 Web 專案
  • ✅ Controller 中「轉到視圖」→ 正確打開 Web 專案文件
  • ✅ Controllers 專案保持純粹,無 View 文件污染
  • ✅ 團隊協作無感知,零配置上手
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved