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


Comments

# by marty1101

奇怪了, 照這麼設也還是不行耶! Virtual Directory還是要設的對吧?

# by Eliza

預設的網站: framework 2.0 子網站: framework 1.1 子網站會因為認不得預設網站中的"inheritInChildApplications"而發生錯誤,請問依您的經驗有沒有a good solution?

# by Jeffrey

to Eliza, 兩個WebApp若CLR版本不同,我會設成各自使用自己的AppPool(http://weblogs.asp.net/owscott/archive/2006/01/26/Running-multiple-versions-of-the-Framework-in-ASP.NET.aspx) 我想這樣應該不會有web.config互擾的問題(純猜測...)

# by maxchiu

這有一篇相關的文章可參考,並附有MSDN相關的參考網址。 http://www.xdevsoftware.com/blog/post/Block-Inheritance-of-Root-webconfig-for-Child-Applications.aspx

# by DeltaCat

对于 .NET 3.5 以及 .NET 4 这个 inheritInChildApplications 已经彻底没有了。 如果 web.config中出现,会报 “找不到属性” 的错误

# by Jeffrey

to DeltaCat, 我查到一篇MS Support在論壇的回覆,雖然在Visual Studio不支援,但可以直接在部署時加上。參考: http://forums.asp.net/t/1215988.aspx

# by 小熊子

若是 ROOT 是 asp.net 2.0 ,子站台要用 asp.net 4.0,記得要將 <configSections> 區段移除,參考保哥文章 https://blog.miniasp.com/post/2011/01/04/How-to-upgrade-ASPNET-20-to-40-manually

Post a comment