配置子目錄Web.config使其消除繼承,用虛擬目錄建立多個網站的方法

來源:互聯網
上載者:User

標籤:blog   http   io   os   使用   ar   for   strong   sp   

ASP.NET提供了強大的Web.config來配置網站,一般來說一個網站只有一個根目錄下的Web.config檔案,有時候我們希望子目錄有著不同的許可權或者參數設定,則可以在相應子目錄增加一個Web.config設定檔,加入我們新的配置參數。這裡需要注意的是,子目錄web.config是繼承父目錄的所有設定的,因此,如果子目錄放的是一個和父目錄裡不一樣的網站,就不能共用父目錄的配置了,否則很可能會衝突。比如很多主機都提供了將網域名稱綁定到子目錄從而能夠建立多個網站的方法,以Godaddy的Windows主機為例,如果我們想通過子目錄建立一個全新的網站,如果不配置好正確的Web.config,很可能會報HTTP 500錯誤Internal Server Error。下面會分析出現該錯誤的原因,並給出了正確的配置方法。

查看HTTP 500錯誤詳情

在自己機器上很容易看到錯誤,但是上傳到遠程主機後,往往只會提示友好的錯誤資訊,讓我們無法知道細節。為了查看錯誤發生的原因,需要顯示錯誤詳情,修改根目錄和子目錄網站的Web.config檔案,在system.webServer節點中添加如下配置,

<httpErrors errorMode="Detailed" />

在system.web節點添加如下配置

<customErrors mode="Off"/>

這樣就可以看到詳細的錯誤來源了。子目錄網站產生500 Internal Server Error錯誤的原因多半是和Web.config配置衝突導致。比如在根目錄定義了一個name為"connStr"的connectioinString節點,如果在子目錄中定義name同樣為"connStr"的connectionString節點,則會出錯,因為任何配置節點都不允許重複鍵的加入,否則會提示Dulplicate key之類的錯誤。那麼怎麼避免子目錄和根目錄的配置衝突呢?一般有兩種解決方案,一種是修改根目錄中的Web.config,使其消除子目錄和根目錄的繼承關係,即子目錄Web.config的配置不受根目錄Web.config的影響;第二種方法是修改子目錄的Web.config,在所有可能和根目錄的Web.config產生衝突的配置add之前先clear或者remove根目錄的配置資訊。下面詳細討論這兩種使子目錄Web.config獨立於根目錄的方法。

子目錄使用獨立Web.config的配置方法

首先看第一種方法,即通過修改根目錄的Web.config消除繼承關係,在根目錄Web.config檔案的system.web或者一切不想讓子目錄繼承的配置節點外面添加一層location如下:

<configuration>  <!--...其他配置...-->  <location path="." inheritInChildApplications="false">    <system.web>    </system.web>  </location>  <!--...其他配置...--></configuration>

這裡inheritInChildApplications很好理解,即是否允許子目錄繼承,預設為true,我們修改為false就可以避免繼承了。這種方法的優點是很簡單,但是不夠靈活,同時面對較複雜的Web.config配置,仍可能會報錯。比如根目錄和子目錄有著不同的system.webServer配置,按照這個方法在根目錄的system.webServer外面加上了location限制,不幸的是,IIS7下會出現500錯誤。當然,也有解決方案,可以參考下面這篇文章。

"SOLVED: IIS7, validateIntegratedModeConfiguration and inheritInChildApplications clash"

接著看第二種方法,不用修改根目錄的Web.config檔案,而是修改子目錄的Web.config。假設根目錄的Web.config設定了一個名為BlogEngine的連接字串,要在子目錄使用另一個名字為BlogEngine的連接字串,就需要先清除已有的連接字串(根目錄繼承下來的connectionString設定),清除所有的配置,可以用clear文法,清除指定名稱的配置,可以用remove文法,如下

<--根目錄的Web.config--><connectionStrings>  <add name="BlogEngine" connectionString="Data Source=localhost\SQLEXPRESS; Initial Catalog=BlogEngine1; User ID=xxx; Password=xxx" providerName="System.Data.SqlClient"/></connectionStrings>
<--子目錄的Web.config(clear方法)--><connectionStrings>  <clear/>  <add name="BlogEngine" connectionString="Data Source=localhost\SQLEXPRESS; Initial Catalog=BlogEngine2; User ID=xxx; Password=xxx" providerName="System.Data.SqlClient"/></connectionStrings>
<--子目錄的Web.config(remove方法)--><connectionStrings>  <remove name="BlogEngine"/>  <add name="BlogEngine" connectionString="Data Source=localhost\SQLEXPRESS; Initial Catalog=BlogEngine2; User ID=xxx; Password=xxx" providerName="System.Data.SqlClient"/></connectionStrings>

這裡只是用connectionString為例,使用時完全可以應用在所有可以配置的節點上,任何配置節點都可以用clear和remove節點將繼承來的配置先清除掉,然後再add新的配置。此方法靈活性更強,同時可以保留根目錄Web.config中的部分共同配置(而無需全部重新設定)。下面是一個複雜些的例子,分別是根目錄和子目錄在system.webServer上的配置。

<--根目錄的Web.config system.webServer配置節點--><modules>  <remove name="ScriptModule"/>  <add name="WwwSubDomainModule" type="BlogEngine.Core.Web.HttpModules.WwwSubDomainModule, BlogEngine.Core"/>  <add name="UrlRewrite" type="BlogEngine.Core.Web.HttpModules.UrlRewrite, BlogEngine.Core"/>  <add name="CompressionModule" type="BlogEngine.Core.Web.HttpModules.CompressionModule, BlogEngine.Core"/>  <add name="ReferrerModule" type="BlogEngine.Core.Web.HttpModules.ReferrerModule, BlogEngine.Core"/>  <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></modules>
<--子目錄的Web.config system.webServer配置節點--><modules runAllManagedModulesForAllRequests="true">  <remove name="WwwSubDomainModule"/>  <remove name="UrlRewrite"/>  <remove name="CompressionModule"/>  <remove name="ReferrerModule"/>  <remove name="ScriptModule"/>  <add name="UrlRewrite" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />  <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></modules><handlers>

可以看到,子目錄將所有根目錄定義的Modules(WwwSubDomainModule,UrlRewrite,CompressionModule,ReferrerModule,ScriptModule)都清除了,添加了自己的UrlRewrite和ScriptModule兩個Module。如此配置既消除了衝突,又可以實現了配置的部分繼承(子目錄只有部分配置和根目錄不同),而第一種方法卻無法實現部分繼承。

配置子目錄Web.config使其消除繼承,用虛擬目錄建立多個網站的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.