讓ASP.NET的Web.Config可以引入外部檔案

今天有一個需要將Web.Config分散到外部檔案的需求,因此在網路上找了一下文件,發現不是討論的很少,就是講得很模糊。經過不斷的TryError後找到了規則,在這邊筆記一下,讓有需要的人取用。

Web.Config extend outher configuration files

Step 1. 找出Web.Config中你要分離的項目,假設我們要搬離的是system.codedom這一整段。

<configuration>
  ...
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" brabra... />
    </compilers>
  </system.codedom>
  ...
</configuration>

Step 2. 將這段搬離到別的目錄,名稱為「test.config」,目錄概況如下:

/root
  -Web.Config
  /extension
    /Slashview
      -test.config

Step 3. 在「test.config」檔案中,存放的XML資訊如下:

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" extension=".cs" brabra... />
  </compilers>
</system.codedom>

Step 4. 回到主要的Web.Config,把剛才搬出去的那段XML刪除,置換成下列這段,就大功告成了。

<system.codedom configSource="extension\Slashview\test.config" />

※ 其實所有的重點在於,你搬出去以及補上延伸指令的根(system.codedom)必須一致,且必須是ASP.NET承認的XML關鍵字集合,這是所有的文章都沒有談論到的事情。

ASP.NET Web.Config Extend Extension Split Setting Files Configuration