共用方式為


重新導向標頭 < redirectHeaders>

概觀

元素 <redirectHeaders> 會指定 Internet Information Services (IIS) 7 將新增至 HTTP 重新導向的自訂 HTTP 標頭集合。

注意

HTTP 標頭是 Web 服務器回應中所傳回的名稱和值組。 不同于 Web 服務器每個回應中傳回的自訂標頭,只有在重新導向發生時才會傳回重新導向標頭。

相容性

版本 備註
IIS 10.0 未在 IIS 10.0 中修改專案 <redirectHeaders>
IIS 8.5 未在 IIS 8.5 中修改專案 <redirectHeaders>
IIS 8.0 未在 IIS 8.0 中修改專案 <redirectHeaders>
IIS 7.5 未在 IIS 7.5 中修改專案 <redirectHeaders>
IIS 7.0 元素 <redirectHeaders><httpProtocol> 元素是在 IIS 7.0 中引進。
IIS 6.0 N/A

安裝程式

元素 <redirectHeaders><httpProtocol> 元素包含在 IIS 7 的預設安裝中。

作法

沒有使用者介面可以將值新增至 <redirectHeaders> IIS 7 的 元素。 For examples of how to add values to the <redirectHeaders> element programmatically, see the Code Samples section of this document.

組態

屬性

無。

子元素

元素 描述
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()