TIPS-切斷web.config的繼承關係
在ASP.NET的設計中,web.config是存在繼承關係的。例如: 我在wwwroot下放的web.config設定,將會影響到子目錄(例如: wwwroot/MySubWebApp)甚至虛擬子目錄下運作的ASPX網頁,即使MySubWebApp已建立成獨立的Web Application時,還是會受到一些影響。 (之前在Sharepoint網站上加掛自己Web AP時,有不少類似經驗)
我在Community Server 2007的網站下,建了一個虛擬目錄MySubWebApp(實體路徑為D:\WWW\MySubWebApp),放入另一個獨立網站的內容,並用IIS設定成獨立的Web Application,瀏覽時會傳回以下錯誤:
Server Error in '/MySubWebApp' Application.
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Could not load file or assembly 'CommunityServer.Components' or one of its dependencies. The system cannot find the file specified. (d:\www\cs2007\web.config line 40)
查了一下,問題出在d:\www\cs2007\web.config 的這一行:
<httpModules>
<add name="CommunityServer" type="CommunityServer.CSHttpModule, CommunityServer.Components" />
</httpModules>
CS2007在它的web.config裡宣告了專用的HttpHandler,而這個設定變成在MySubWebApp裡也生效,於是ASP.NET會試圖在D:\WWW\MySubWebApp\bin裡尋找CommunityServer.Components.dll。根本是八竿子打不著的東西,當然找不到而以錯誤收場。
要解決這個問題,我們可以切斷這層繼承關係。在web.config裡可宣告location inheritInChildApplications來阻止繼承。
不過,這是個神祕的Attribute,在Visual Studio中不會提示,MSDN上也找不到這項Attrubute。在CS2007的web.config上,用<location path="." inheritInChildApplications="false">把<system.web>包起來,問題就解決囉!