Condividi tramite


Metodo IHttpResponse::SetErrorDescription

Specifica la descrizione dell'errore personalizzato.

Sintassi

virtual HRESULT SetErrorDescription(  
   IN PCWSTR pszDescription,  
   IN DWORD cchDescription,  
   IN BOOL fHtmlEncode = TRUE  
) = 0;  

Parametri

pszDescription
[IN] Puntatore a una stringa contenente la descrizione dell'errore personalizzata.

cchDescription
[IN] Oggetto DWORD contenente la lunghezza, espressa in caratteri, della stringa in pszDescription.

fHtmlEncode
[IN] true se la stringa in deve essere codificata per HTML; in pszDescription caso contrario, false.

Valore restituito

Oggetto HRESULT. I valori possibili includono, ma non sono limitati a, quelli indicati nella tabella seguente.

Valore Descrizione
S_OK Indica che l'operazione è riuscita.
ERROR_INVALID_PARAMETER Indica che è stato passato un valore non valido in uno dei parametri.

Commenti

Gli sviluppatori usano il SetErrorDescription metodo per specificare la descrizione dell'errore personalizzata. IIS visualizza la descrizione dell'errore personalizzata come parte delle informazioni dettagliate inviate a un client Web quando IIS restituisce un messaggio di errore.

Esempio

L'esempio di codice seguente illustra come creare un modulo HTTP che esegue le attività seguenti:

  1. Recupera un puntatore a un'interfaccia IHttpResponse usando il metodo IHttpContext::GetResponse .

  2. Recupera il codice di stato HTTP corrente usando il metodo IHttpResponse::GetStatus .

  3. Se il codice di stato HTTP corrente è un errore 404.0, recupera la descrizione dell'errore personalizzato corrente usando il metodo IHttpResponse::GetErrorDescription .

  4. Se non è attualmente definita alcuna descrizione dell'errore personalizzata, imposta la descrizione dell'errore personalizzata utilizzando il SetErrorDescription metodo .

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

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

    REQUEST_NOTIFICATION_STATUS
    OnSendResponse(
        IN IHttpContext * pHttpContext,
        IN ISendResponseProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );

        // Retrieve a a pointer to the current response.
        IHttpResponse * pHttpResponse = pHttpContext->GetResponse();

        // Test for errors.
        if (NULL != pHttpResponse)
        {
            USHORT uStatusCode = 0;
            USHORT uSubStatus = 0;

            // Retrieve the current HTTP status code.
            pHttpResponse->GetStatus(&uStatusCode,&uSubStatus);

            // Process only 404.0 errors.
            if (uStatusCode==404 && uSubStatus==0)
            {
                DWORD cchDescription = 0;
                
                // Retrieve the current error description.
                PCWSTR pwszErrorDescription =
                    pHttpResponse->GetErrorDescription(&cchDescription);

                // Process only if no error description is currently defined.
                if (cchDescription==0)
                {
                    // Define the new error description.
                    PCWSTR wszNewDescription =
                        L"The file that you requested cannot be found.";
                    // Configure the new error description.
                    pHttpResponse->SetErrorDescription(
                        wszNewDescription,wcslen(wszNewDescription),TRUE);               
                }
            }
        }
        // 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_SEND_RESPONSE,
        0
    );
}

Il modulo deve esportare la funzione RegisterModule . È possibile esportare questa funzione creando un file di definizione del modulo (con estensione def) per il progetto oppure è possibile compilare il modulo usando l'opzione /EXPORT:RegisterModule . Per altre informazioni, vedere Procedura dettagliata: Creazione di un modulo HTTP Request-Level tramite codice nativo.

Facoltativamente, è possibile compilare il codice usando la __stdcall (/Gz) convenzione di chiamata anziché dichiarare in modo esplicito la convenzione di chiamata per ogni funzione.

Requisiti

Tipo Descrizione
Client - IIS 7.0 in Windows Vista
- IIS 7.5 in Windows 7
- IIS 8.0 in Windows 8
- IIS 10.0 in Windows 10
Server - IIS 7.0 in Windows Server 2008
- IIS 7.5 in Windows Server 2008 R2
- IIS 8.0 in Windows Server 2012
- IIS 8.5 in Windows Server 2012 R2
- IIS 10.0 in Windows Server 2016
Prodotto - 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
Intestazione Httpserv.h

Vedere anche

Interfaccia IHttpResponse
Metodo IHttpResponse::GetErrorDescription