有许多方式利用这种和APPCMD
C#/ VB.NET/JavaScript的/ VBScript中做:
Custom Headers (IIS.NET)
要做到这一点使用PowerShell和Microsoft.Web.Administration
装配:
[Reflection.Assembly]::Load("Microsoft.Web.Administration, Version=7.0.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")
$serverManager = new-object Microsoft.Web.Administration.ServerManager
$siteConfig = $serverManager.GetApplicationHostConfiguration()
$httpProtocolSection = $siteConfig.GetSection("system.webServer/httpProtocol", "Default Web Site")
$customHeadersCollection = $httpProtocolSection.GetCollection("customHeaders")
$addElement = $customHeadersCollection.CreateElement("add")
$addElement["name"] = "X-Custom-Name"
$addElement["value"] = "MyCustomValue"
$customHeadersCollection.Add($addElement)
$serverManager.CommitChanges()
这将导致<location>
路径applicationHost.config
下列要求:
<location path="Default Web Site">
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Custom-Name" value="MyCustomValue" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
要使用新的IIS 7 PowerShell Snap-In这样做在PowerShell中:
add-webconfiguration `
-filter /system.webServer/httpProtocol/customHeaders `
-location "Default Web Site" `
-pspath "IIS:" `
-value @{name='X-MyHeader';value='MyCustomHeaderValue'} `
-atindex 0
这将配置一个<location>
路径applicationHost.config
与下列:
<location path="Default Web Site">
<system.webServer>
<httpProtocol>
<customHeaders>
<clear />
<add name="X-MyHeader" value="MyCustomHeaderValue" />
<add name="X-Powered-By" value="ASP.NET" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
背蜱在每一行的末尾指示线延续。上面给出的两个示例在Windows 2008 Server SP2上进行了测试。
来源
2009-11-11 17:59:20
Kev
这是预期的行为 - IIS 6管理兼容性的重点在于允许您使用IIS 6脚本管理IIS 7。 – 2009-11-11 18:44:59
但是,我同意使用MWA名称空间或PS管理单元更好。通过System.DirectoryServices使用ADSI可以解决applicationHost.config文件的问题。例如,在操作脚本映射时会创建AboCustomerMapper对象。这些可能会导致长期头痛。 – Kev 2009-11-11 18:57:09