HttpResponseBase.AddOnSendingHeaders(Action<HttpContextBase>) 方法


註冊 ASP.NET 執行階段會立即在回應標頭傳送給這個要求之前叫用的回呼。Registers a callback that the ASP.NET runtime will invoke immediately before response headers are sent for this request.

 virtual System::Web::ISubscriptionToken ^ AddOnSendingHeaders(Action<System::Web::HttpContextBase ^> ^ callback);
public virtual System.Web.ISubscriptionToken AddOnSendingHeaders (Action<System.Web.HttpContextBase> callback);
abstract member AddOnSendingHeaders : Action<System.Web.HttpContextBase> -> System.Web.ISubscriptionToken
override this.AddOnSendingHeaders : Action<System.Web.HttpContextBase> -> System.Web.ISubscriptionToken
Public Overridable Function AddOnSendingHeaders (callback As Action(Of HttpContextBase)) As ISubscriptionToken



回呼方法。The callback method.



傳回表示訂閱 OnSendingHeaders 虛擬事件的 ISubscriptionToken 物件。Returns an ISubscriptionToken object that represents the subscription to the OnSendingHeaders pseudo event.


虛擬事件 OnSendingHeaders 與 IHttpModule 層級管線事件不同,因為它是每個要求的訂用帳戶,而不是每個應用程式的訂用帳戶。The pseudo event OnSendingHeaders differs from the IHttpModule-level pipeline event in that it is a per-request subscription rather than a per-application subscription. 其目的是回呼可能會修改回應狀態碼,或可能設定回應 cookie 或標頭。The intent is that the callback may modify the response status code or may set a response cookie or header. 其他使用注意事項和注意事項:Other usage notes and caveats:

  • 只有當 IIS 在整合式管線模式管線中執行,而且只有在尚未傳送目前要求的回應標頭時,此方法才會生效。This method is effective only when IIS runs in integrated pipeline mode pipeline and only if response headers haven't yet been sent for the current request.

  • ASP.NET 執行時間不保證叫用回呼的任何相關執行緒。The ASP.NET runtime does not guarantee anything about the thread that the callback is invoked on. 例如,如果正在執行背景排清,則會在背景執行緒中以同步方式叫用回呼。For example, the callback may be invoked synchronously in a background thread if a background flush is being performed. Current 不保證可在這類執行緒中使用。Current is not guaranteed to be available in such a thread.

  • 回呼不能呼叫任何操作回應實體主體或導致排清的方法。The callback must not call any method that manipulates the response entity body or that results in a flush. 例如,回呼不能呼叫 Redirect ,因為該方法可能會操作回應實體主體。For example, the callback must not call Redirect, as that method may manipulate the response entity body.

  • 回呼必須只包含短時間執行的同步程式碼。The callback must contain only short-running synchronous code. 嘗試叫用非同步作業或等候這類作業時,可能會產生鎖死。Attempting to invoke an asynchronous operation or wait on such an operation could result in a deadlock.

  • 回呼不能擲回例外狀況;否則行為未定義。The callback must not throw an exception; otherwise behavior is undefined.