預設的 ASP.NET MVC 專案在 Visual Studio 中建置 (Build) 時並不會對 Views 進行編譯,所以很有可能在 Views 中寫錯程式碼而不自知,甚至於有可能讓網站上線時掛掉。為了避免這個問題,我們必須手動設定專案檔讓建置時也一併編譯 Views 中所有網頁,讓開發人員得以儘早得知程式錯誤。
要設定讓 ASP.NET MVC 專案能在 Visual Studio 中能自動編譯 Views 中的網頁,必須要修改 ASP.NET MVC 專案檔 ( *.csproj ),如果你有安裝 PowerCommands for Visual Studio 2008 的話可參考以下步驟進行修改,或自行用文字編輯器做修改。
1. 開啟/編輯專案檔

2. 找到 <MvcBuildViews>false</MvcBuildViews> 片段,並將 false 改成 true 即可。

3. 重新載入專案即大功告成

不過,若要開啟這個設定會導致專案建置時間拉長,如果專案小有規模的話,會導致編譯時間過久,開發階段開啟這個選項可能會有點浪費時間,建議在進行 Release build 時再開啟即可,若要這麼做有個很簡單的小技巧可以設定,說明如下。
重複步驟一,並將 <MvcBuildViews>false</MvcBuildViews> 複製到另外兩個 <PropertyGroup> 中,並將 Release 的那個 <MvcBuildViews> 改成 true 即可,如下圖示:

後續就不用再修改專案檔,只要切換 Solution Configurations 即可套用不同的 MvcBuildViews 設定:

善用 Visual Studio 的 Solution Configurations 可以簡化不少工作,建議新手有機會多研習手邊的開發工具,慢慢的開發效率就會提升。
相關連結