Thursday, July 10, 2008 - 文章

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>包起來,問題就解決囉!

搜尋

Go

<July 2008>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
 
RSS
【工商服務】
最新回應

Tags 分類檢視
關於作者

一個醉心技術又酷愛分享的Coding魔人,十年的IT職場生涯,寫過系統、管過專案, 也帶過團隊,最後還是無怨無悔地選擇了技術鑽研這條路,近年來則以做一個"有為的中年人"自許。

文章典藏
其他功能

這個部落格


BlogLook Score and Rank

Syndication