IHttpResponse::Redirect Method
Redirects the client to a specified URL.
Syntax
virtual HRESULT Redirect(
IN PCSTR pszUrl,
IN BOOL fResetStatusCode = TRUE,
IN BOOL fIncludeParameters = FALSE
) = 0;
Parameters
pszUrl
[IN] A pointer to a string that contains the URL for redirection.
fResetStatusCode
[IN] true
to set the HTTP status code to a 302 status; false
to return an HTML message that indicates the page has been moved.
fIncludeParameters
[IN] true
to pass the query string from the original HTTP request to the redirected URL; otherwise, false
.
Return Value
An HRESULT
. Possible values include, but are not limited to, those in the following table.
Value | Description |
---|---|
S_OK | Indicates that the operation was successful. |
ERROR_INVALID_PARAMETER | Indicates that a specified parameter was not valid. |
ERROR_NOT_ENOUGH_MEMORY | Indicates that there is insufficient memory to perform the operation. |
Remarks
When the fResetStatusCode
parameter is true
, the Redirect
method automatically redirects a client to a URL specified by the pszUrl
parameter. When fResetStatusCode
is false
, the Redirect
method returns an HTML message stating that the URL has moved to the new location. If the URL specified by the pszUrl
parameter is a relative path, the URL will be converted to an absolute URL within the request domain.
Note
If you do not return RQ_NOTIFICATION_FINISH_REQUEST from your module after you call the Redirect
method, the Redirect
method will not automatically clear the response buffer. In this situation, subsequent processing may add information to the response buffer, and you may receive unexpected results if you do not manually clear the response.
Note
The Redirect
method does not flush the response entity to the client, and any data in the response will be removed when you call the Redirect
method. If the response has already been flushed to the client, IIS will have sent the existing headers and data to the client, and the Redirect
method will not redirect the client to the new URL.
Example
The following code example demonstrates how to use the Redirect
method to create an HTTP module that redirects a client to a relative URL on the Web server.
#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
);
}
Your module must export the RegisterModule function. You can export this function by creating a module definition (.def) file for your project, or you can compile the module by using the /EXPORT:RegisterModule
switch. For more information, see Walkthrough: Creating a Request-Level HTTP Module By Using Native Code.
You can optionally compile the code by using the __stdcall (/Gz)
calling convention instead of explicitly declaring the calling convention for each function.
Requirements
Type | Description |
---|---|
Client | - IIS 7.0 on Windows Vista - IIS 7.5 on Windows 7 - IIS 8.0 on Windows 8 - IIS 10.0 on Windows 10 |
Server | - IIS 7.0 on Windows Server 2008 - IIS 7.5 on Windows Server 2008 R2 - IIS 8.0 on Windows Server 2012 - IIS 8.5 on Windows Server 2012 R2 - IIS 10.0 on Windows Server 2016 Technical Preview |
Product | - 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 |
Header | Httpserv.h |