POST Get Redirect with WCF WebAPI

Topics: Web Api
Feb 8, 2012 at 6:12 PM

Hi All,

    is any one implemented POST-Get-Redirect scenoria with WCF WebAPI . is so how to do it.

Feb 8, 2012 at 6:13 PM
Edited Feb 8, 2012 at 6:14 PM

Do you mean where every POST is redirected to the same request, but as GET?

That doesn't apply to APIs... that only applies to Web UIs.

Feb 8, 2012 at 6:27 PM

No , Once the post a resource back to the server you want to redirect the Client to the different page ,Its like giving 301,302 to the Client

Feb 8, 2012 at 6:28 PM

Sounds like your answer is "yes", not "no".

I don't think that that methodology should be anywhere near WebApi.

Feb 8, 2012 at 6:32 PM

:) .. 

May be a small problem and solution approach will help you understand more.. 

Lets imagine i have a Customer representation from the server to the client and there is an edit happened in client and i will post the edited data to the server and ask the client to move to the different Resource . How will you do through REST..

Feb 8, 2012 at 6:35 PM

You don't do that with REST.

With rest when you edit a resource, you return the edited resource in the response.

Feb 8, 2012 at 6:39 PM

Anyways. You can do it like this:

 

public class myservice
{
  public HttpResponseMessage<int> Post(int x)
  {
    var m = new HttpResponseMessage(HttpStatusCode.Redirect);
    m.Headers.Location = new Uri("");
    m.Content = new ObjectContent<int>(1);
    return m;
  }
}

Feb 8, 2012 at 6:46 PM

You mean to say Put can't return 301 or 302 as the success code .Is there any reason in not doing it

Feb 8, 2012 at 6:47 PM

I didn't say can't, I meant it shouldn't.

If a user asks for one resource, why are you giving them another one. That doesn't make any sense.

Feb 8, 2012 at 6:52 PM

IMHO ,Hypermedia as engine of application state gives the client a direction to the which is the next logical to take after each action . According to me PUT Verb is a Action on the resource should also say can say which is the next action the client should take.Correct me if i am wrong

Feb 8, 2012 at 6:56 PM

Well this is why there are so many derivations of what "REST" means ;)

One person implements it one way, the other another. If you follow my example above, that should give you your intended result.