在 IIS 'set config' 中添加多个自定义 headers 的语法

Syntax for adding multiple custom headers in IIS 'set config'

我有以下 IIS 配置作为发布管道的一部分:

set config "$(WebsiteName)$(WebAppName)" -section:system.webServer/httpProtocol /+"customHeaders.[name='Version-Info',value='$(Release.DefinitionName):$(Release.ReleaseName):$(Release.EnvironmentName)']"

如何扩充 customheaders 配置,以便我可以规定多个自定义 headers,而不仅仅是下面的 Version-Info

customHeaders.[name='Version-Info',value='$(Release.DefinitionName):$(Release.ReleaseName):$(Release.EnvironmentName)']

我测试了不同的脚本以在命令行中添加 headers。看来唯一的办法就是多次执行命令来添加多个 headers.

Appcmd

appcmd.exe set config "a" -section:system.webServer/httpProtocol /+"customHeaders.[name='header1',value='value1']" 

appcmd.exe set config "a" -section:system.webServer/httpProtocol /+"customHeaders.[name='header2',value='value2']" 

appcmd.exe set config "a" -section:system.webServer/httpProtocol /+"customHeaders.[name='header3',value='value3']" 

Powershell

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/a'  -filter "system.webServer/httpProtocol/customHeaders" -name "." -value @{name='header1';value='value1'}

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/a'  -filter "system.webServer/httpProtocol/customHeaders" -name "." -value @{name='header2';value='value2'}

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/a'  -filter "system.webServer/httpProtocol/customHeaders" -name "." -value @{name='header3';value='value3'}

Javascript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/a";

var httpProtocolSection = adminManager.GetAdminSection("system.webServer/httpProtocol", "MACHINE/WEBROOT/APPHOST/a");

var customHeadersCollection = httpProtocolSection.ChildElements.Item("customHeaders").Collection;

var addElement = customHeadersCollection.CreateNewElement("add");
addElement.Properties.Item("name").Value = "header1";
addElement.Properties.Item("value").Value = "value1";
customHeadersCollection.AddElement(addElement);


var addElement1 = customHeadersCollection.CreateNewElement("add");
addElement1.Properties.Item("name").Value = "header2";
addElement1.Properties.Item("value").Value = "value2";
customHeadersCollection.AddElement(addElement1);


var addElement2 = customHeadersCollection.CreateNewElement("add");
addElement2.Properties.Item("name").Value = "header3";
addElement2.Properties.Item("value").Value = "value3";
customHeadersCollection.AddElement(addElement2);


adminManager.CommitChanges();

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample {

    private static void Main() {
        
        using(ServerManager serverManager = new ServerManager()) { 
            Configuration config = serverManager.GetWebConfiguration("a");
            
            ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol");
            
            ConfigurationElementCollection customHeadersCollection = httpProtocolSection.GetCollection("customHeaders");
            
            ConfigurationElement addElement = customHeadersCollection.CreateElement("add");
            addElement["name"] = @"header1";
            addElement["value"] = @"value1";
            customHeadersCollection.Add(addElement);
            
            ConfigurationElement addElement1 = customHeadersCollection.CreateElement("add");
            addElement1["name"] = @"header2";
            addElement1["value"] = @"value2";
            customHeadersCollection.Add(addElement1);
            
            ConfigurationElement addElement2 = customHeadersCollection.CreateElement("add");
            addElement2["name"] = @"header3";
            addElement2["value"] = @"value3";
            customHeadersCollection.Add(addElement2);
            
            serverManager.CommitChanges();
        }
    }
}