IHttpResponse::Redirect 方法
將用戶端重新導向至指定的 URL。
語法
virtual HRESULT Redirect(
IN PCSTR pszUrl,
IN BOOL fResetStatusCode = TRUE,
IN BOOL fIncludeParameters = FALSE
) = 0;
參數
pszUrl
[IN]包含重新導向 URL 的字串指標。
fResetStatusCode
[IN] true
表示將 HTTP 狀態碼設定為 302 狀態; false
表示頁面已移動的 HTML 訊息。
fIncludeParameters
[IN] true
將查詢字串從原始 HTTP 要求傳遞至重新導向的 URL;否則為 false
。
傳回值
HRESULT
。 可能的值包括 (但不限於) 下表中的這些值。
值 | 描述 |
---|---|
S_OK | 表示作業成功。 |
ERROR_INVALID_PARAMETER | 表示指定的參數無效。 |
ERROR_NOT_ENOUGH_MEMORY | 表示記憶體不足,無法執行作業。 |
備註
fResetStatusCode
當 參數為 true
時, Redirect
方法會自動將用戶端重新導向至 參數所 pszUrl
指定的 URL。 當 為 false
時 fResetStatusCode
,方法會 Redirect
傳回 HTML 訊息,指出 URL 已移至新位置。 如果參數指定的 pszUrl
URL 是相對路徑,URL 將會轉換成要求網域內的絕對 URL。
注意
如果您在呼叫 Redirect
方法之後未從模組傳回RQ_NOTIFICATION_FINISH_REQUEST,方法 Redirect
將不會自動清除回應緩衝區。 在此情況下,後續的處理可能會將資訊新增至回應緩衝區,如果您未手動清除回應,您可能會收到非預期的結果。
注意
方法 Redirect
不會將回應實體排清至用戶端,而且當您呼叫 Redirect
方法時,將會移除回應中的任何資料。 如果回應已經排清至用戶端,IIS 就會將現有的標頭和資料傳送至用戶端,而且 Redirect
方法不會將用戶端重新導向至新的 URL。
範例
下列程式碼範例示範如何使用 Redirect
方法來建立 HTTP 模組,將用戶端重新導向至 Web 服務器上的相對 URL。
#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>
// Create the module class.
class MyHttpModule : public CHttpModule
{
public:
REQUEST_NOTIFICATION_STATUS
OnBeginRequest(
IN IHttpContext * pHttpContext,
IN IHttpEventProvider * pProvider
)
{
UNREFERENCED_PARAMETER( pProvider );
// Retrieve a pointer to the response.
IHttpResponse * pHttpResponse = pHttpContext->GetResponse();
// Test for an error.
if (pHttpResponse != NULL)
{
// Set the response header.
HRESULT hr = pHttpResponse->Redirect("/example/",true,true);
// Test for an error.
if (FAILED(hr))
{
// Set the error status.
pProvider->SetErrorStatus( hr );
}
// End additional processing.
return RQ_NOTIFICATION_FINISH_REQUEST;
}
// Return processing to the pipeline.
return RQ_NOTIFICATION_CONTINUE;
}
};
// Create the module's class factory.
class MyHttpModuleFactory : public IHttpModuleFactory
{
public:
HRESULT
GetHttpModule(
OUT CHttpModule ** ppModule,
IN IModuleAllocator * pAllocator
)
{
UNREFERENCED_PARAMETER( pAllocator );
// Create a new instance.
MyHttpModule * pModule = new MyHttpModule;
// Test for an error.
if (!pModule)
{
// Return an error if the factory cannot create the instance.
return HRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY );
}
else
{
// Return a pointer to the module.
*ppModule = pModule;
pModule = NULL;
// Return a success status.
return S_OK;
}
}
void Terminate()
{
// Remove the class from memory.
delete this;
}
};
// Create the module's exported registration function.
HRESULT
__stdcall
RegisterModule(
DWORD dwServerVersion,
IHttpModuleRegistrationInfo * pModuleInfo,
IHttpServer * pGlobalInfo
)
{
UNREFERENCED_PARAMETER( dwServerVersion );
UNREFERENCED_PARAMETER( pGlobalInfo );
// Set the request notifications and exit.
return pModuleInfo->SetRequestNotifications(
new MyHttpModuleFactory,
RQ_BEGIN_REQUEST,
0
);
}
您的模組必須匯出 RegisterModule 函式。 您可以為專案建立模組定義 (.def) 檔案,或使用 參數編譯模組 /EXPORT:RegisterModule
來匯出此函式。 如需詳細資訊,請參閱 逐步解說:使用機器碼建立 Request-Level HTTP 模組。
您可以選擇性地使用呼叫慣例編譯器代碼, __stdcall (/Gz)
而不是明確宣告每個函式的呼叫慣例。
規格需求
類型 | 描述 |
---|---|
Client | - Windows Vista 上的 IIS 7.0 - Windows 7 上的 IIS 7.5 - Windows 8 上的 IIS 8.0 - Windows 10上的 IIS 10.0 |
伺服器 | - Windows Server 2008 上的 IIS 7.0 - Windows Server 2008 R2 上的 IIS 7.5 - Windows Server 2012 上的 IIS 8.0 - Windows Server 2012 R2 上的 IIS 8.5 - Windows Server 2016上的 IIS 10.0 |
產品 | - IIS 7.0、IIS 7.5、IIS 8.0、IIS 8.5、IIS 10.0 - IIS Express 7.5、IIS Express 8.0、IIS Express 10.0 |
標頭 | Httpserv.h |