Redirecting From a WCF REST Service
REST is based on HTTP and revolves around using the standard HTTP verbs GET, POST, PUT as well as standard HTTP error codes. The error codes that have to do with redirection are in the 300 block. Today we’ll focus on 302 which is the status code for a redirection (other 300 codes, such as 301 indicate that the URI moved permanently).
The following code will redirect the client with a 302 status code:
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Redirect; WebOperationContext.Current.OutgoingResponse.Location = "/someOtherPage.aspx"; return null;
For an additional measure we’ll return null as well so that there is no output emitted to the client. The end result will be that the headers include the 302 status code, the location to redirect to and the body of the response will be empty.
Comments are closed.


April 2, 2010 - 10:55 am
Is there a way to redirect from a regular wcf service? I’m using the method you mentioned above, but with a regular wcf service and I’m getting error: “The content type text/html of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8)”.
Here’s a little explanation on what I’m trying to do:
A website makes a call to my wcf service to retrieve data. The wcf service returns the serialized objects to the caller and then automatically redirects the calling page to a new web page. Is this all possible with a WCF service or should I use a regular website? I’m fine with using a WCF REST service if it can be done.
April 2, 2010 - 11:18 am
I do not believe it is possible to redirect from a SOAP based WCF service because the client is expecting a SOAP response and the redirect sends them to an HTML page. Hence the error message you received. Sometimes you get that message if there is a unhandled exception in your service which returns the generic aspx error page to the client.
This could be done by having a web page call your service using server side code then redirecting from that page after the service call completes. If the website calling your service is not under your control then you would be better off using a REST based service since those play better with the HTTP standards (supporting redirects, etc).