共用方式為


IHttpCoNtext::ExecuteRequest 方法

執行子要求。

語法

virtual HRESULT ExecuteRequest(  
   IN BOOL fAsync,  
   IN IHttpContext* pHttpContext,  
   IN DWORD dwExecuteFlags,  
   IN IHttpUser* pHttpUser,  
   OUT BOOL* pfCompletionExpected = NULL  
) = 0;  

參數

fAsync
[IN]一律 true (指定非同步執行) 。

pHttpContext
[IN]要執行的子 IHttpCoNtext 指標。

dwExecuteFlags
[IN] DWORD ,包含要求執行旗標。

pHttpUser
[IN]要求的 IHttpUser 指標。 (選用)

pfCompletionExpected
[OUT] true 如果非同步完成仍在擱置中,則為 ;否則為 false 。 (選用)

傳回值

HRESULT。 可能的值包括 (但不限於) 下表中的這些值。

描述
S_OK 表示作業成功。
ERROR_NOT_SUPPORTED 表示不支援要求 (例如,設定為 falsefAsync 子要求未從父要求複製) 。
ERROR_STACK_OVERFLOW 表示要求超過遞迴子要求的限制。

備註

方法 ExecuteRequest 會執行 參數中 pHttpContextIHttpCoNtext介面所指定的子要求。 您必須使用 IHttpCoNtext::CloneCoNtext 方法來建立此要求內容。

重要

嘗試執行父要求未複製的子要求會傳回ERROR_NOT_SUPPORTED。

每個子內容只能執行一次,雖然子要求可以遞迴方式巢狀化。

注意

遞迴子要求的限制為 10。

方法 ExecuteRequest 僅支援非同步作業,以防止耗盡執行緒集區。

重要

嘗試執行同步子要求會傳回ERROR_NOT_SUPPORTED。

您可以在 中 dwExecuteFlags 指定執行旗標,以控制子要求的執行行為。 下表列出這些旗標的可能值。

描述
EXECUTE_FLAG_NO_HEADERS 隱藏子要求的 HTTP 標頭。
EXECUTE_FLAG_IGNORE_CURRENT_INTERCEPTOR 忽略此要求鏈結目前的腳本對應處理常式。
EXECUTE_FLAG_IGNORE_APPPOOL 即使子要求不在相同的應用程式集區中,仍執行要求。
EXECUTE_FLAG_DISABLE_CUSTOM_ERROR 停用子要求的自訂錯誤。
EXECUTE_FLAG_SAME_URL 子要求的 URL 與父要求相同。 注意: 腳本對應處理常式會使用此旗標來轉送執行。
EXECUTE_FLAG_BUFFER_RESPONSE 請勿排清子回應;會傳回父要求的回應。
EXECUTE_FLAG_HTTP_CACHE_ELIGIBLE 子回應仍可透過Http.sys進行快取。 注意: 執行子要求時,預設會停用快取。

如果您為 指定 IHttpUser 介面 pHttpUser ,則會略過子要求的驗證。

範例

下列程式碼範例示範如何建立執行下列工作的 HTTP 模組:

  1. 模組會註冊 RQ_MAP_PATH 通知。

  2. 模組會建立包含OnMapPathOnAsyncCompletion方法的CHttpModule類別。

  3. 當 Web 用戶端要求 URL 時,IIS 會呼叫模組的 OnMapPath 方法。 這個方法會執行下列工作:

    1. 測試以查看目前要求的 URL 是否符合網站根目錄中的兩個特定 URL。 如果 URL 符合其中一個指定的 URL,模組會使用 IHttpContext::CloneContext 方法來建立目前要求的複本。

    2. 呼叫複製的 IHttpRequest::SetUrl 方法,將複製的 URL 設定為 /example/default.aspx。

    3. ExecuteRequest呼叫 方法來執行子要求。

    4. 測試非同步完成。 如果非同步完成擱置中,模組會將處理傳回至整合式要求處理管線。 如果沒有,模組會釋放複製的內容。

  4. 如果需要非同步完成,IIS 會呼叫模組的 OnAsyncCompletion 方法。 這個方法會釋放複製的內容。

  5. 模組會 CHttpModule 從記憶體中移除 類別,然後結束。

#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>

// Create the module class.
class MyHttpModule : public CHttpModule
{

private:

    // Create a pointer for a child request.
    IHttpContext * m_pChildRequestContext;

public:

    MyHttpModule(void)
    {
        m_pChildRequestContext = NULL;
    }

    REQUEST_NOTIFICATION_STATUS
    OnMapPath(
        IN IHttpContext * pHttpContext,
        IN IMapPathProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );

        HRESULT hr;
        BOOL fCompletionExpected;

        // Retrieve a pointer to the URL.
        PCWSTR pwszUrl = pProvider->GetUrl();

        // Only process requests for the root.
        if (0 == wcscmp(pwszUrl,L"/") || 0 == wcscmp(pwszUrl,L"/default.aspx"))
        {            
            // Clone the current context.
            hr = pHttpContext->CloneContext(
                CLONE_FLAG_BASICS, &m_pChildRequestContext );
            
            // Test for a failure.
            if (FAILED(hr))
            {
                goto Failure;
            }
            
            // Test for an error.
            if ( NULL != m_pChildRequestContext )
            {
                // Set the URL for the child request.
                hr = m_pChildRequestContext->GetRequest()->SetUrl(
                    "/example/default.aspx",
                    (DWORD)strlen("/example/default.aspx"),false);
            
                // Test for a failure.
                if (FAILED(hr))
                {
                    goto Failure;
                }
                
                // Execute the child request.
                hr = pHttpContext->ExecuteRequest(
                    TRUE, m_pChildRequestContext,
                    0, NULL, &fCompletionExpected );
                
                // Test for a failure.
                if (FAILED(hr))
                {
                    goto Failure;
                }
                
                // Test for pending asynchronous operations.
                if (fCompletionExpected)
                {
                    return RQ_NOTIFICATION_PENDING;
                }

            }

 Failure:
            // Test for a child request.
            if (NULL != m_pChildRequestContext)
            {
                // Release the child request.
                m_pChildRequestContext->ReleaseClonedContext();
                m_pChildRequestContext = NULL;
            }
        }
        
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }
    
    REQUEST_NOTIFICATION_STATUS
        OnAsyncCompletion(
        IN IHttpContext * pHttpContext,
        IN DWORD dwNotification,
        IN BOOL fPostNotification,
        IN IHttpEventProvider * pProvider,
        IN IHttpCompletionInfo * pCompletionInfo
        )
    {
        // Test for a child request.
        if (NULL != m_pChildRequestContext)
        {
            // Release the child request.
            m_pChildRequestContext->ReleaseClonedContext();
            m_pChildRequestContext = NULL;
        }
        // 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 we 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 );

    return pModuleInfo->SetRequestNotifications(
        new MyHttpModuleFactory,
        RQ_MAP_PATH,
        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

另請參閱

IHttpCoNtext 介面
IHttpCoNtext::CloneCoNtext 方法
IHttpCoNtext::GetParentCoNtext 方法
IHttpCoNtext::ReleaseClonedCoNtext 方法