在 IIS 中編輯配置自定義部分
我正在研究由 5 個不同的網站和一些共享程序集組成的大型 ASP.NET 項目(我們使用 ASP.NET 3.5)。最近我
web.config在每個站點的文件中添加了自定義部分。當我部署所有這些應用程序時,每個站點都單獨部署在同一個應用程序池下。有什麼方法可以在站點級別的 IIS 中編輯此部分,就像您可以ConnectionString為每個站點編輯部分一樣?我添加的部分都是這樣的:
<sectionGroup name="RegistriesCustomSettings"> <section name="RegistriesSettings" type="Registries.Business.Utilities.RegistriesConfigurations"/> </sectionGroup > <RegistriesCustomSettings> <RegistriesSettings ContextCommandTimeout="30" logLinq="true" DisplayUser="true" BaseReportPath="/DDD/" ReportingServer="http://patriot-regdev:8000/ReportServer" TopInstitution="1000001" /> </RegistriesCustomSettings>我們使用的是 IIS 7.0、2008 RC 2。
是的,有一種方法可以通過擴展 IIS 配置架構來做到這一點。
- 創建一個名為的文件
RegistriesSchema.xml,然後複製並粘貼以下 XML:<configSchema> <sectionSchema name="RegistriesCustomSettings"> <element name="RegistriesSettings"> <attribute name="ContextCommandTimeout" type="int" validationType="integerRange" validationParameter="1,600" allowInfinite="true" defaultValue="30" /> <attribute name="logLinq" type="bool" defaultValue="True" /> <attribute name="DisplayUser" type="bool" defaultValue="True" /> <attribute name="BaseReportPath" type="string" validationType="nonEmptyString" /> <attribute name="ReportingServer" type="string" validationType="nonEmptyString" /> <attribute name="TopInstitution" type="string" validationType="nonEmptyString" /> </element> </sectionSchema> </configSchema>
IisSchema.exe獲取從此處呼叫的工具的副本:解壓縮並確保 exe 和 xml 架構文件在同一個文件夾中。 3. 從管理員命令行(即
cmd.exe使用“以管理員身份執行”打開):IISSCHEMA.EXE /install RegistriesSchema.xml這將做兩件事:
- 將架構文件放入
%systemroot%\system32\inetsrv\config\schema- 將以下 XML 添加到
applicationHost.config:<section name="RegistriesCustomSettings" overrideModeDefault="允許" allowDefinition="到處" />4. 啟動 IIS 管理器並打開您網站的功能設置並打開配置編輯器:  5. 選擇部分下拉列表:  如果一切正常,您應該看到“RegistriesCustomSettings”,選擇此項。 6. 您現在可以編輯這些設置,它們將被添加到您網站的`web.config`文件中:  這只是一個展示,因此架構設置可能不太正確,可能需要一些微調。 **怎麼辦`<sectionGroup name="RegistriesCustomSettings">`?:** 您仍然需要將`configSection/sectionGroup`xml 添加到`web.config`每個站點的文件中,*或者*您可以將其添加到您正在使用的任何版本的 ASP.NET 的根`machine.config`文件中,即: 對於 .NET Framework 2.0(也適用於 .NET3.0 和 3.5):%systemroot%\Microsoft.NET\Framework\v2.050727\CONFIG\machine.config
%systemroot%\Microsoft.NET\Framework64\v2.050727\CONFIG\machine.config
對於 .NET Framework 4.0:%systemroot%\Microsoft.NET\Framework\v4.0.30319\CONFIG\machine.config
%systemroot%\Microsoft.NET\Framework64\v4.0.30319\CONFIG\machine.config
如果您將程序集`configSection/sectionGroup`放在`machine.config`文件中,則無需在每個站點的`web.config`. 如果相當多的站點將使用此程序集,那麼這可能會節省很多時間。 **更新:** IIS7.5 配置編輯器中似乎存在錯誤或限制。看來,如果您在站點文件中有自己的自定義`configSections` `<sectionGroup>`或聲明,這會破壞 IIS7.5 配置編輯器。我試圖深入了解這一點:`<section>``web.config` > > [ASP.NET 自定義配置部分聲明中斷 IIS 管理器配置編輯器](https://serverfault.com/questions/267556/asp-net-custom-configuration-section-declaration-breaks-iis-manager-configuration) > > > --- **更新 2:** 我認為這方面的 MS 文件有點虛假,特別是在您的自定義配置部分需要由 ASP.NET 使用並且可以在 IIS 管理器配置編輯器中編輯的情況下。訣竅似乎是在文件中聲明模式如下`RegistriesSchema.xml`:<configSchema> <sectionSchema name=“RegistriesCustomSettings/RegistriesSettings”> <attribute name=“ContextCommandTimeout” type=“int” validationType=“integerRange” validationParameter=“1,600” allowInfinite=“true” defaultValue=“30” /> <attribute name=“logLinq” type=“bool” defaultValue=“True” /> <attribute name=“DisplayUser” type=“bool” defaultValue=“True” /> <attribute name=“BaseReportPath” type=“string” validationType=“nonEmptyString” /> <attribute name=“ReportingServer” type=“string” validationType=“nonEmptyString” /> <attribute name=“TopInstitution” type=“string” validationType=“nonEmptyString” /> </sectionSchema> </configSchema>
此外,重要的是,從以下位置刪除部分參考`applicationHost.config`:<section name=“RegistriesCustomSettings” overrideModeDefault=“Allow” allowDefinition=“Everywhere” />
這不是必需的。 此外,您實際上不需要使用該`iisschema.exe`工具,只需獲取 NotePad2 的副本(它是一個 64 位編輯器,您需要它來編輯任何內容`inetsrv\config`)並`RegistriesSchema.xml`直接在`inetsrv\config\schema`. --- 您可以在此處找到有關擴展 IIS7 架構的更多資訊: > > [使用 MWA 擴展 IIS 7.0 架構和訪問自定義部分](http://learn.iis.net/page.aspx/242/extending-iis7-schema-and-accessing-the-custom-sections-using-mwa/) > > > 您可以查看現有架構文件以了解有關如何建構這些設置的更多資訊。它們可以在以下位置找到:%systemroot%\system32\inetsrv\config\schema
警告:上面的範例在 Windows 7 x64 Ultimate 上的 IIS7.5 x64 RTM 上進行了測試。您提到您正在執行一個候選版本,因此您的里程可能會因此而變化。