重定向标头 <redirectHeaders>
概述
概述 <redirectHeaders>
元素指定 Internet Information Services (IIS) 7 将添加到 HTTP 重定向的自定义 HTTP 标头集合。
注意
HTTP 标头是在 Web 服务器的响应中返回的名称和值对。 自定义响应标头在 Web 服务器的每个响应中都会返回,与此不同的是,重定向响应标头仅在发生重定向时才在响应中返回。
兼容性
版本 | 说明 |
---|---|
IIS 10.0 | <redirectHeaders> 元素在 IIS 10.0 中未进行修改。 |
IIS 8.5 | <redirectHeaders> 元素在 IIS 8.5 中未进行修改。 |
IIS 8.0 | <redirectHeaders> 元素在 IIS 8.0 中未进行修改。 |
IIS 7.5 | <redirectHeaders> 元素在 IIS 7.5 中未进行修改。 |
IIS 7.0 | IIS 7.0 中引入了 <httpProtocol> 元素的 <redirectHeaders> 元素。 |
IIS 6.0 | 空值 |
安装
在 IIS 7 的默认安装中包含 <httpProtocol>
元素的 <redirectHeaders>
元素。
操作方式
没有用于将模块添加到 IIS 7 的 <redirectHeaders>
元素的用户界面。 有关如何以编程方式将值添加到 <redirectHeaders>
元素的示例,请参阅本文档的代码示例部分。
配置
特性
无。
子元素
元素 | 说明 |
---|---|
add |
可选元素。 将响应标头添加到 <redirectHeaders> 集合。 |
clear |
可选元素。 从 <redirectHeaders> 集合中移除对响应标头的所有引用。 |
remove |
可选元素。 从 <redirectHeaders> 集合中移除对响应标头的引用。 |
配置示例
以下配置示例指定自定义 HTTP 标头和值,仅在 IIS 7 重定向请求时才会添加到响应中。
<configuration>
<system.webServer>
<httpProtocol>
<redirectHeaders>
<add name="X-Custom-Redirect-Header" value="MyRedirectValue" />
</redirectHeaders>
</httpProtocol>
</system.webServer>
</configuration>
注意
以下默认 <httpProtocol>
元素是在 IIS 7 的 ApplicationHost.config 文件中配置的。
<httpProtocol>
<customHeaders>
<clear />
<add name="X-Powered-By" value="ASP.NET" />
</customHeaders>
<redirectHeaders>
<clear />
</redirectHeaders>
</httpProtocol>
代码示例
以下代码示例指定自定义 HTTP 标头和值,仅在 IIS 7 重定向请求时才会添加到响应中。
AppCmd.exe
appcmd.exe set config "Default Web Site" -section:system.webServer/httpProtocol /+"redirectHeaders.[name='X-Custom-Redirect-Header',value='MyRedirectValue']"
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("Default Web Site");
ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol");
ConfigurationElementCollection redirectHeadersCollection = httpProtocolSection.GetCollection("redirectHeaders");
ConfigurationElement addElement = redirectHeadersCollection.CreateElement("add");
addElement["name"] = @"X-Custom-Redirect-Header";
addElement["value"] = @"MyRedirectValue";
redirectHeadersCollection.Add(addElement);
serverManager.CommitChanges();
}
}
}
VB.NET
Imports System
Imports System.Text
Imports Microsoft.Web.Administration
Module Sample
Sub Main()
Dim serverManager As ServerManager = New ServerManager
Dim config As Configuration = serverManager.GetWebConfiguration("Default Web Site")
Dim httpProtocolSection As ConfigurationSection = config.GetSection("system.webServer/httpProtocol")
Dim redirectHeadersCollection As ConfigurationElementCollection = httpProtocolSection.GetCollection("redirectHeaders")
Dim addElement As ConfigurationElement = redirectHeadersCollection.CreateElement("add")
addElement("name") = "X-Custom-Redirect-Header"
addElement("value") = "MyRedirectValue"
redirectHeadersCollection.Add(addElement)
serverManager.CommitChanges()
End Sub
End Module
JavaScript
var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site";
var httpProtocolSection = adminManager.GetAdminSection("system.webServer/httpProtocol", "MACHINE/WEBROOT/APPHOST/Default Web Site");
var redirectHeadersCollection = httpProtocolSection.ChildElements.Item("redirectHeaders").Collection;
var addElement = redirectHeadersCollection.CreateNewElement("add");
addElement.Properties.Item("name").Value = "X-Custom-Redirect-Header";
addElement.Properties.Item("value").Value = "MyRedirectValue";
redirectHeadersCollection.AddElement(addElement);
adminManager.CommitChanges();
VBScript
Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"
Set httpProtocolSection = adminManager.GetAdminSection("system.webServer/httpProtocol", "MACHINE/WEBROOT/APPHOST/Default Web Site")
Set redirectHeadersCollection = httpProtocolSection.ChildElements.Item("redirectHeaders").Collection
Set addElement = redirectHeadersCollection.CreateNewElement("add")
addElement.Properties.Item("name").Value = "X-Custom-Redirect-Header"
addElement.Properties.Item("value").Value = "MyRedirectValue"
redirectHeadersCollection.AddElement(addElement)
adminManager.CommitChanges()