James Black
James Black

Reputation: 41858

Redirect to another URL from a WCF REST function without returning HTTP 302

I have a REST service that gets a GET request, and I want it to be able to redirect to one of three urls, without having to send an http 302 error and the new url.

    [OperationContract(Name = "MyService")]
    [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/myservice/{option}")]
    public System.IO.Stream MyService(String option)

This is how I have it defined, but when I look at WebOperationContext.Current.OutgoingResponse I don't have a method for redirect.

Is there any way to do this automatically? The reason I am hoping to do this is that this webservice may be called by a program, and it would be simpler on the program to not have to catch the 302 error and then do the redirect manually.

It appears the correct way is to follow this: http://christopherdeweese.com/blog2/post/redirecting-from-a-wcf-rest-service

and do this:

WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Redirect;
WebOperationContext.Current.OutgoingResponse.Location = "/someOtherPage.aspx";
return null; 

I just hope to find another solution.

Upvotes: 0

Views: 6307

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

Redirect is always client side feature. The server just informs client that the resource is elsewhere. There is nothing like server side redirect except completely hiding this redirection into separate service call from your current operation.

Btw. REST is about uniform interface and redirects (3xx) are part of this interface so if you create REST service and you want such feature you should use it in common way. REST client is responsible for handling the uniform interface correctly.

Upvotes: 4

Related Questions