IHttpServer::GetManagedType 方法
擷取特定網站和統一資源識別項 (URI) 的 Managed 類型。
語法
virtual HRESULT GetManagedType(
IN DWORD dwSiteId,
IN PCWSTR pszVirtualPath,
__out PWSTR pszManagedType,
IN OUT DWORD* pcchManagedType
) = 0;
參數
dwSiteId
[IN]中 pszVirtualPath
URI 的網站識別碼。
pszVirtualPath
[IN]包含 URI 之字串的指標。
pszManagedType
[__out]預先配置字串緩衝區的指標。
pcchManagedType
[IN][OUT]緩衝區的 pszManagedType
長度,以位元組為單位。
傳回值
HRESULT
。 可能的值包括 (但不限於) 下表中的這些值。
值 | 描述 |
---|---|
S_OK | 表示作業成功。 |
E_INVALIDARG | 表示其中一個參數 (傳遞不正確值,例如,其中一個指標會設定為 Null) 。 |
E_OUTOFMEMORY | 表示沒有足夠的可用記憶體可執行作業。 |
ERROR_INVALID_PARAMETER | 表示在其中一個參數中傳遞了不正確值。 |
ERROR_INSUFFICIENT_BUFFER | 表示沒有足夠的緩衝區空間可執行作業。 |
備註
方法 GetManagedType
會 type
擷取要求處理常式的屬性,該處理常式會處理 和 pszVirtualPath
參數所 dwSiteId
指定的特定虛擬路徑。 方法接著會在 參數所 pszManagedType
指定的緩衝區中傳回這項資訊。
重要
呼叫端必須配置 的 pszManagedType
緩衝區。 如果呼叫端傳遞此參數的 Null,方法會傳回E_INVALIDARG。
type
要求處理常式的屬性位於 <handlers>
ApplicationHost.config檔案的 區段中。 此屬性包含要求處理常式的.NET Framework命名空間清單。 例如,根據預設,「TraceHandler-Integrated」 要求處理常式具有 type
「System.Web.Handlers.TraceHandler」 的屬性。
方法 IHttpServer::GetManagedType
與IScriptMapInfo::GetManagedType方法不同,因為 IHttpServer::GetManagedType
方法可以擷取任何虛擬路徑的屬性,而 IScriptMapInfo::GetManagedType
方法只會 type
擷取 type
IScriptMapInfo介面的屬性。
範例
下列程式碼範例示範如何使用 GetManagedType
方法來建立 HTTP 模組,以擷取 /default.aspx URI 的 Managed 類型。
此課程模組會完成下列步驟:
擷取目前要求的月臺識別碼。
擷取要求之 Managed 類型所需的長度。
為 Managed 型別配置緩衝區。
擷取 Managed 類型。
將結果傳回至 Web 用戶端,然後結束。
#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>
// Create a pointer for the global server interface.
IHttpServer * g_pHttpServer = NULL;
// Create the module class.
class MyHttpModule : public CHttpModule
{
public:
REQUEST_NOTIFICATION_STATUS
OnBeginRequest(
IN IHttpContext * pHttpContext,
IN IHttpEventProvider * pProvider
)
{
UNREFERENCED_PARAMETER( pProvider );
// Create an HRESULT to receive return values from methods.
HRESULT hr;
// Retrieve the site ID.
DWORD dwSiteId = pHttpContext->GetSite()->GetSiteId();
// Create buffers to store the managed type.
PWSTR pwszManagedType =
(PWSTR) pHttpContext->AllocateRequestMemory( 1 );
char* pszManagedType = NULL;
DWORD cchManagedType = 0;
// Test for an error.
if (NULL != pwszManagedType)
{
// Retrieve the size of the managed type.
hr = g_pHttpServer->GetManagedType(
dwSiteId,L"/default.aspx",
pwszManagedType,&cchManagedType);
// Test for an error.
if ((HRESULT_CODE(hr)==ERROR_INSUFFICIENT_BUFFER) && (cchManagedType>0))
{
// Allocate space for the managed type.
pwszManagedType =
(PWSTR) pHttpContext->AllocateRequestMemory(
(cchManagedType*2)+1 );
// Test for an error.
if (NULL == pwszManagedType)
{
// Set the error status.
hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
pProvider->SetErrorStatus( hr );
}
else
{
// Retrieve the managed type.
hr = g_pHttpServer->GetManagedType(
dwSiteId,L"/default.aspx",
pwszManagedType,&cchManagedType);
// Test for an error.
if (SUCCEEDED(hr))
{
// Clear the existing response.
pHttpContext->GetResponse()->Clear();
// Set the MIME type to plain text.
pHttpContext->GetResponse()->SetHeader(
HttpHeaderContentType,"text/plain",
(USHORT)strlen("text/plain"),TRUE);
// Allocate space for the managed type.
pszManagedType =
(char*) pHttpContext->AllocateRequestMemory(
(DWORD) wcslen(pwszManagedType)+1 );
// Test for an error.
if (NULL == pszManagedType)
{
// Set the error status.
hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
pProvider->SetErrorStatus( hr );
}
else
{
// Convert the WCHAR string to a CHAR string.
wcstombs(pszManagedType,
pwszManagedType,wcslen(pwszManagedType));
// Return the managed type to the client.
WriteResponseMessage(pHttpContext,"Managed type: ",
(cchManagedType>1) ? pszManagedType : "n/a");
}
// End additional processing.
return RQ_NOTIFICATION_FINISH_REQUEST;
}
}
}
}
// Return processing to the pipeline.
return RQ_NOTIFICATION_FINISH_REQUEST;
}
private:
// Create a utility method that inserts a name/value pair into the response.
HRESULT WriteResponseMessage(
IHttpContext * pHttpContext,
PCSTR pszName,
PCSTR pszValue
)
{
// Create an HRESULT to receive return values from methods.
HRESULT hr;
// Create a data chunk.
HTTP_DATA_CHUNK dataChunk;
// Set the chunk to a chunk in memory.
dataChunk.DataChunkType = HttpDataChunkFromMemory;
// Buffer for bytes written of data chunk.
DWORD cbSent;
// Set the chunk to the first buffer.
dataChunk.FromMemory.pBuffer =
(PVOID) pszName;
// Set the chunk size to the first buffer size.
dataChunk.FromMemory.BufferLength =
(USHORT) strlen(pszName);
// Insert the data chunk into the response.
hr = pHttpContext->GetResponse()->WriteEntityChunks(
&dataChunk,1,FALSE,TRUE,&cbSent);
// Test for an error.
if (FAILED(hr))
{
// Return the error status.
return hr;
}
// Set the chunk to the second buffer.
dataChunk.FromMemory.pBuffer =
(PVOID) pszValue;
// Set the chunk size to the second buffer size.
dataChunk.FromMemory.BufferLength =
(USHORT) strlen(pszValue);
// Insert the data chunk into the response.
hr = pHttpContext->GetResponse()->WriteEntityChunks(
&dataChunk,1,FALSE,TRUE,&cbSent);
// Test for an error.
if (FAILED(hr))
{
// Return the error status.
return hr;
}
// Return a success status.
return S_OK;
}
};
// 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 );
// Store the pointer for the global server interface.
g_pHttpServer = 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 |